[英]django basic pagination problem
i have a microblog app, and i'm trying to paginate the entries, to show only 10 per page, for example. 我有一个微博应用程序,我正在尝试对条目进行分页,例如每页只显示10个。 though i've followed the tutorial, my pagination doesn't seem t be working.
虽然我已经按照教程,我的分页似乎没有工作。
the listing function looks like that: 列表函数看起来像这样:
def listing(request):
blog_list = Blog.objects.all()
paginator = Paginator(blog_list, 10)
try:
page = int(request.GET.get('page','1'))
except ValueError:
page = 1
try:
posts = paginator.page(page)
except (EmptyPage, InvalidPage):
posts = paginator.page(paginator.num_pages)
return render_to_response('profile/publicProfile.html', {"posts": posts})
and in my template: 在我的模板中:
<div class="pagination">
<span class="step-links">
{% if posts.has_previous %}
<a href="?page={{ posts.previous_page_number }}">previous</a>
{% endif %}
<span class="current">
Page {{ posts.number }} of {{ posts.paginator.num_pages }}.
</span>
{% if object.has_next %}
<a href="?page={{ posts.next_page_number }}">next</a>
{% endif %}
</span>
thanks! 谢谢!
You can use django-pagination which makes it possible to implement pagination without writing a single line of Python code, you only pass list of all objects to template (ie blog_list = Blog.objects.all()
in your case), and then use three tags in you template: 您可以使用django-pagination ,这样可以在不编写单行Python代码的情况下实现分页,您只需将所有对象的列表传递给模板(即在您的情况下为
blog_list = Blog.objects.all()
),然后使用模板中的三个标签:
{% load pagination_tags %}
{% autopaginate blog_list 10 %}
{% paginate %}
返回带有 paginate_by
参数的object_list泛型视图,而不是返回render_to_response
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.