Django分页显示问题:显示所有页码 [英] Django Pagination Display Issue: all the page numbers show up
问题描述
有什么方法可以使django分页的页面显示更好?我按照[doc] [1]进行创建,但希望有一种简单的方法来组织页码显示.
is there any way to make page display of django pagination better? I followed the [doc][1] to create it, but hoping there is simple way to organize page number display.
当前,它显示所有页面,例如我有10页,然后
上一页1 2 3 4 5 6 7 8 9 10下一页
Currently, it shows all the pages, say I have 10 pages, then
prev 1 2 3 4 5 6 7 8 9 10 next
如果有100,那么它将显示所有100,这非常疯狂.
If there is 100, then it will show all 100, which is pretty crazy.
有没有一种简单的方法可以将其显示得更短?
Is there any way simple way to display it shorter?
示例:
上一页1 2 3 ... 67 ...下一个98、99、100(当前页面为67)
prev 1 2 3 ... 67 ... 98, 99, 100 next (67 is the current page)
上一页1 2 3 ... 65 66 67 68 69 ... 100下一个
prev 1 2 3 ... 65 66 67 68 69 ... 100 next
它不必看起来像上面的示例,但是只是不想让它显示没有限制的每个页码.
It doesn't have to look like above examples, but just don't want it to show every single page number without limits.
就像文档一样,我使用以下代码创建了分页.
Just like the doc, I created my pagination using below codes.
模板文件
{% if is_paginated %}
<div id="pagination">
<ul>
{% if page_obj.has_previous %}
<li> <a href="?page={{page_obj.previous_page_number}}">Previous</a> </li>
{% else %}
<li> Previous</li>
{% endif %}
{% for page_number in paginator.num_pages|template_range %}
{% ifequal page_number page_obj.number %}
<li class="currentpage">{{page_number}}</li>
{% else %}
<li> <a href="?page={{page_number}}">{{page_number}}</a> </li>
{% endifequal %}
{% endfor %}
{% if page_obj.has_next %}
<li> <a href="?page={{page_obj.next_page_number}}">Next</a></li>
{% else %}
<li> Next </li>
{% endif %}
</ul>
</div>
{% endif %}
Views.py
news = News.active.all().order_by("-created_at")
paginator = Paginator(news, 15)
is_paged = False
page = None
try:
paginator.validate_number(currpage)
except (EmptyPage, InvalidPage):
#return bad_or_missing(request, ("Invalid page number"))
currpage = paginator.num_pages
is_paged = paginator.num_pages > 1
page = paginator.page(currpage)
ctx = RequestContext(request, {
'all_news_list' : page.object_list,
'is_paginated' : is_paged,
'page_obj' : page,
'paginator' : paginator,
'featured_categories' : featured_categories,
})
response = render_to_response(template_name, context_instance=ctx)
return response
谢谢.
推荐答案
我使用了" Paginator标记"成功实现您正在寻找的分页样式(通常称为"Digg样式"分页).用法在文章" Django中的分页食谱中进行了说明.
I have used the "Paginator Tag" with success to achieve the style of pagination you are looking for (often called "Digg-style" pagination). Usage is explained in the article "A Recipe for Pagination in Django".
这篇关于Django分页显示问题:显示所有页码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!