Django 分页显示问题:所有页码都显示出来

发布于 2024-11-02 13:54:13 字数 2010 浏览 1 评论 0原文

有没有办法让django分页的页面显示更好?我按照[doc][1]创建它,但希望有一种简单的方法来组织页码显示。

目前,它显示了所有页面,假设我有 10 页,那么
prev 1 2 3 4 5 6 7 8 9 10 next

如果有 100 个,那么它将显示全部 100 个,这相当疯狂。

有什么简单的方法可以让它显示得更短吗?

例如:

上一页 1 2 3 ... 67 ... 98, 99, 100 下一页(67 是当前页)

上一页 1 2 3 ... 65 66 67 68 69 ... 100 next

它不必看起来像上面的示例,但只是不希望它无限制地显示每个页码。

就像文档一样,我使用以下代码创建了分页。

模板文件

{% 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

谢谢。

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.

Currently, it shows all the pages, say I have 10 pages, then
prev 1 2 3 4 5 6 7 8 9 10 next

If there is 100, then it will show all 100, which is pretty crazy.

Is there any way simple way to display it shorter?

example:

prev 1 2 3 ... 67 ... 98, 99, 100 next (67 is the current page)

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.

Template file

{% 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

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

就像说晚安 2024-11-09 13:54:13

我已经使用“Paginator Tag”成功地实现了您正在寻找的分页样式(通常称为“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".

谁的年少不轻狂 2024-11-09 13:54:13

我使用了下面的包,它很好用,而且很简单。
https://github.com/jamespacileo/django-pure-pagination

它解决了常见问题是不显示模板中的每个页面,而是显示一个范围。

输入图像这里的描述

设置使这样的实现变得非常容易:

PAGINATION_SETTINGS = {
    'PAGE_RANGE_DISPLAYED':10,
    'MARGIN_PAGES_DISPLAYED':2,

    'SHOW_FIRST_PAGE_WHEN_INVALID':正确,
}

I have used the following package, and it is nice and simple to use.
https://github.com/jamespacileo/django-pure-pagination

It solves the common issue of not displaying every single page in the template, but rather showing a range.

enter image description here

The setting make such an implementation very easy:

PAGINATION_SETTINGS = {
    'PAGE_RANGE_DISPLAYED': 10,
    'MARGIN_PAGES_DISPLAYED': 2,

    'SHOW_FIRST_PAGE_WHEN_INVALID': True,
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文