Python Django Paginator Paging __python

Source: Internet
Author: User

Write a blog when you use the paging, recorded.

From Django.core.paginator import paginator

is to use this paginator class.

Paginator (Object_list, Per_page, [Orphans=0, Allow_empty_first_page=true])

Parameter object_list (total data) and per_page (amount of data per page) are required parameters

Orphans (minimum number of last page objects, defaults to 0) if you want to avoid the last page showing too little. You can use this value. Then the last page of data is automatically moved forward one page. For example, a total of 23 data. Show 10.orphans=3 per page So, the first page is 10, the second page is 13.

Allow_empty_first_page (indicates whether the first page can be empty) if it is False and ' object_list ' is empty, then a emptypage exception is triggered.


Simple usage

View Layer Reference:

From Django.core.paginator import paginator                                                              
    def index (Request): Home_display_columns = Category.objects.filter (home_display=true) nav_display_columns = Category.obj              
                                                                                 
    Ects.filter (Nav_display=true)
                                                                                 
    Index_article = Article.objects.filter (published=1). order_by ('-pub_date ')                                                                    
    Limit = 2 Paginator = Paginator (index_article, limit) #每页两条数据 page = Request.                                          
    Get.get (' page ', 1) #QueryDict objects, if there is no corresponding page key, return the default 1. Item_info = paginator.page (page) #根据索引page, returns the page numberAccording to, if not present, cause invalidpage anomaly return render (Request, ' index.html ', {' Hom                              
        E_display_columns ': home_display_columns, ' nav_display_columns ': Nav_display_columns, ' Index_article ': index_article, ' I Tem_info ': Item_info,})

Then look at the template layer, using the semantic UI

Template

<div class= "UI Pagination Center aligned menu" >
  {% if item_info.has_previous%}
  <a class= "item" href= "? Page={{item_info.previous_page_number} ' > prev </a>
  {% endif%}
  {% for page_num in item_ Info.paginator.page_range%}
  <a class= "item" href= "Page={{page_num}}" >
    {{page_num}}}
  </a>
  {% endfor%}
  {% if Item_info.has_next%}
  <a class= "Item" href= "? Page={{item_info.next_page_number}" > next page </a>
  {% endif%}
</div>
A basic paging module is complete.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.