Django pagination and “current page”

后端 未结 3 797
旧时难觅i
旧时难觅i 2021-02-07 13:16

I\'m currently developing a Django application which will make use of the infamous \"pagination\" technique. I\'m trying to figure out how the django.core.paginator module work

3条回答
  •  抹茶落季
    2021-02-07 13:56

    Django, at least from version 1.2, allows us to complete this task by using pure default pagination template tags.

    {% for page in article_list.paginator.page_range %}
      {% if page == article_list.number %}
        {{ page }}
      {% else %}
        {{ page }}
      {% endif %}
    {% endfor %}
    

    Where article_list is instance of

    paginator = Paginator(article_list, 20)
        try:
            article_list = paginator.page(int(page))
        except (EmptyPage, InvalidPage):
            article_list = paginator.page(paginator.num_pages)
    

提交回复
热议问题