Display only some of the page numbers by django pagination

前端 未结 8 1283
盖世英雄少女心
盖世英雄少女心 2020-12-07 10:34

I am using the django paginator in the template. Its working ok, but not good when there\'s large numbers of pages.

views.py:

def blog(request):
             


        
8条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-07 11:20

    Inspired by @Pavel1114 and @Inti. Removed the arrow at the beginning and at the end as they are not that useful. Added The first page and last page for faster accessing.

    • {{paginator.count}} total
    • {% if data.number|add:'-4' > 1 %}
    • 1
    • {% endif %} {% for i in data.paginator.page_range %} {% if data.number == i %}
    • {{ i }}
    • {% elif i > data.number|add:'-3' and i < data.number|add:'3' %}
    • {{ i }}
    • {% endif %} {% endfor %} {% if data.paginator.num_pages > data.number|add:'4' %}
    • {{ data.paginator.count }}
    • {% endif %}

提交回复
热议问题