如何在基于类的通用视图中使用分页?
我尝试对基于类的通用视图实现分页,但以我的方式实现,它不起作用。
urls
url(r'^cat/(?P<category>[\w+\s]*)/page(?P<page>[0-9]+)/$',
CategorizedPostsView.as_view(), {'paginate_by': 3}),
view
class CategorizedPostsView(ListView):
template_name = 'categorizedposts.djhtml'
context_object_name = 'post_list'
def get_queryset(self):
cat = unquote(self.kwargs['category'])
category = get_object_or_404(ParentCategory, category=cat)
return category.postpages_set.all()
template
<div class="pagination">
<span class="step-links">
{% if post_list.has_previous %}
<a href="?page={{ post_list.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ post_list.number }} of {{ post_list.paginator.num_pages }}.
</span>
{% if post_list.has_next %}
<a href="?page={{ post_list.next_page_number }}">next</a>
{% endif %}
</span>
</div>
当我尝试获取 http:// 127.0.0.1:8000/cat/category_name/?page=1 甚至 http:// 127.0.0.1:8000/cat/category_name/ 时,出现 404 异常。
如何在基于类的通用视图中正确使用分页?
I try to implement pagination to class-based generic view and in way I did it, it's not works.
urls
url(r'^cat/(?P<category>[\w+\s]*)/page(?P<page>[0-9]+)/
view
class CategorizedPostsView(ListView):
template_name = 'categorizedposts.djhtml'
context_object_name = 'post_list'
def get_queryset(self):
cat = unquote(self.kwargs['category'])
category = get_object_or_404(ParentCategory, category=cat)
return category.postpages_set.all()
template
<div class="pagination">
<span class="step-links">
{% if post_list.has_previous %}
<a href="?page={{ post_list.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ post_list.number }} of {{ post_list.paginator.num_pages }}.
</span>
{% if post_list.has_next %}
<a href="?page={{ post_list.next_page_number }}">next</a>
{% endif %}
</span>
</div>
When I try to get the http:// 127.0.0.1:8000/cat/category_name/?page=1 or even http:// 127.0.0.1:8000/cat/category_name/ I got 404 exception.
How to use pagination in class-based generic views in right way?
,
CategorizedPostsView.as_view(), {'paginate_by': 3}),
view
template
When I try to get the http:// 127.0.0.1:8000/cat/category_name/?page=1 or even http:// 127.0.0.1:8000/cat/category_name/ I got 404 exception.
How to use pagination in class-based generic views in right way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嘿,
ListView
已经有一个 kwargpaginate_by
,所以只需将其传入即可。尝试这样的事情:
对于你的模板,你可以尝试这样的事情:
hey there is already a kwarg
paginate_by
for theListView
so just pass it in.try something like this:
and for your template you could try something like: