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.