Django Implements paging functionality

Source: Internet
Author: User

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">&laquo;</a>8 {% endif%}9 {% if not articles.has_previous%}Ten             <ahref="" >&laquo;</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}}" >&raquo;</a> - {% endif%} - {% if not articles.has_next%} in             <ahref="" >&raquo;</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

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.