Paging Effect:
View Code:
1 #-*-coding:utf-8-*-2 fromDjango.shortcutsImportrender,get_object_or_4043 fromDjango.core.paginatorImportPaginator,pagenotaninteger,emptypage4 5 from. ModelsImportarticle6 7 #Create your views here.8 9 defIndex (Request):Ten #latest_article_list = Article.objects.order_by (' Update ') [: 5] One #context = {' latest_article_list ': latest_article_list} A #return render (Request, ' blog/index.html ', context) -Article_list = Article.objects.all (). Order_by ('cre_date') -Paginator = Paginator (article_list,2)#Show 2 articles per page the -page = Request. Get.get ('page') - - Try: +Articles =paginator.page (page) - exceptPagenotaninteger: + #the page number is not an integer and returns to the first page. AArticles = Paginator.page (1) at exceptEmptypage: -Articles =paginator.page (paginator.num_pages) - - returnRender (Request,'blog/index.html', {'Articles': articles})
Paginator is a paging instance, page is the page number parameter that the link passes to the back end, and articles is an instance of each page.
In the following example, Paginator is dividing all the articles (Article_list) by two pages per page, divided into 3 pages. Page is the page number of the front-end request. Articles is the specific page number of the article (2 articles) that is returned based on the requested page number.
The properties and methods of Paginator and articles are described in the documentation: https://docs.djangoproject.com/en/1.8/topics/pagination/
Front-End Code:
1 <!--page Out -2 <nav>3 <Divclass= "Pagination Pagination-right">4 <ul>5 <Li>6 {% if articles.has_previous%}7 <ahref= "? page={{Articles.previous_page_number}}"class= "Active">«</a>8 {% endif%}9 {% if not articles.has_previous%}Ten <ahref="" >«</a> One {% endif%} A </Li> - - <Li> the {% for I in articles.paginator.page_range%} - <Li{% if Articles.number== I%}class= "Active"{% endif%}> - <ahref= "? page={{i}}">{{i}} - + </a> - </Li> + {% endfor%} A </Li> at - <Li> - {% if Articles.has_next%} - <ahref= "? page={{Articles.next_page_number}}" >»</a> - {% endif%} - {% if not articles.has_next%} in <ahref="" >»</a> - {% endif%} to </Li> + - <Li> the Total {{articles.paginator.num_pages}} page * </Li> $ </ul>Panax Notoginseng </Div> - </nav> the <!--ending sub-page -
Django Implements paging functionality