Display only some of the page numbers by django pagination

前端 未结 8 1271
盖世英雄少女心
盖世英雄少女心 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:23

    Gonna throw this in. I came up with it because it lets you know there are more pages on either side.

      {% if page_obj.has_previous %}
    • {% else %}
    • {% endif %} {% if page_obj.number|add:'-4' > 1 %}
    • {% endif %} {% for i in page_obj.paginator.page_range %} {% if page_obj.number == i %}
    • {{ i }} (current)
    • {% elif i > page_obj.number|add:'-5' and i < page_obj.number|add:'5' %}
    • {{ i }}
    • {% endif %} {% endfor %} {% if page_obj.paginator.num_pages > page_obj.number|add:'4' %}
    • {% endif %} {% if page_obj.has_next %}
    • {% else %}
    • {% endif %}

    And it looks like this:

提交回复
热议问题