Django paginator page range for not displaying all numbers

跟風遠走 提交于 2019-12-04 10:02:08
David542

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.

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.

Another quick solution that can be used(+ and - 5 limit for example):

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

See if https://medium.com/@sumitlni/paginate-properly-please-93e7ca776432 helps. The code uses 10 for "neighbors" but you could change it to 5 when using the tag.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!