Django Paginator页面范围,用于不显示所有数字 [英] Django paginator page range for not displaying all numbers

查看:199
本文介绍了Django Paginator页面范围,用于不显示所有数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的网站上有一个分页,但是它向我显示了1-19的每个页面,我只希望显示5个页面.

I have a pagination in my site but it shows me every page like 1-19, i only want to display only 5 pages.

我该怎么做?

views.py

    paginator = Paginator(object_list, new_number_of_list)

    page = request.GET.get('page')

    try:
        Items = paginator.page(page)
    except PageNotAnInteger:
        Items = paginator.page(1)
    except EmptyPage:
        Items = paginator.page(paginator.num_pages)

    variables = RequestContext(request, {"Items": Items,
                                         "ForItemCount": ForItemCount,
                                         "page": page,
                                         })
    return render(request, 'templates/Core/resource_base.html',
                           variables)

我的pagination.html

my pagination.html

<div class="pagination p1">
  <span class="step-links">
    <ul>

          {% for l in  Items.paginator.page_range %}
            <li><a href="?page={{forloop.counter}}">{{forloop.counter}}</a></li>
          {% endfor %}



    </ul>
  </span>
</div>

推荐答案

您已经有了{{ forloop.counter }},可以将其限制为五个.

You already have the {{ forloop.counter }} in there, you can use that to limit it to five.

      {% for l in  Items.paginator.page_range %}
        {% if forloop.counter < 5 %}
            <li><a href="?page={{forloop.counter}}">{{forloop.counter}}</a></li>
        {% endif %}
      {% endfor %}

这是另一种解决方案,如果您感兴趣的话,它可以为您提供更多选择:

Here is another solution, which might give you some more options if you're interested: Django Pagination Display Issue: all the page numbers show up.

最后,这是一个很好的教程,帮助您快速了解django提供的分页速度:

And finally, here is a very nice tutorial on getting up to speed with the pagination that django offers out of the box: https://simpleisbetterthancomplex.com/tutorial/2016/08/03/how-to-paginate-with-django.html.

这篇关于Django Paginator页面范围,用于不显示所有数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆