Django achieves pagination

Source: Internet
Author: User

Django version 1.4.5

This method is in the official document and can be implemented in the same way after 1.4.

The default page has been created, but the page is required.

First, View

1 From Django. core. paginator import paginator, emptypage, pagenotaninteger 2 3 def listing (request): 4 contact_list = contacts. objects. all () 5 paginator = paginator (contact_list, 25) # each page displays 25 contacts 6 pages = request. get. get ('page') 7 Try: 8 contacts = paginator. page (PAGE) 9 handle T pagenotaninteger: 10 # If a non-numeric parameter is received, the first page is displayed. Because it is transmitted by get, you can change 11 contacts = paginator in the address bar. page (1) 12 TB t emptypage: 13 # The page is out of range. The last page 14 Contacts = paginator is displayed. page (paginator. num_pages)
15 return render_to_response('list.html ', {"contacts": contacts })

The contacts here is an existing model and needs to be imported.

Next, add the relevant code in HTML to display the page effect, as shown below:

1 {% for contact in contacts %} 2 {# Each "Contact" is a contact model object. #} 3 {contact. full_name | Upper }}< br/> 4... <! -- Here is the loop output of your contacts --> 5 {% endfor %} 6 7 <! -- The following is the paging code --> 8 <Div class = "pagination"> 9 <SPAN class = "step-links"> 10 {% if contacts. has_previous %} 11 <a href = "? Page = {contacts. previus_page_number }}"> previous </a> 12 {% endif %} 13 <SPAN class = "current"> 14 page {contacts. number }}of {contacts. paginator. num_pages }}. 15 </span> 16 {% if contacts. has_next %} 17 <a href = "? Page = {contacts. next_page_number} "> next </a> 18 {% endif %} 19 </span> 20 </div>

It should be hard to understand.

For details about the paginator object, refer to the official documentation ......

Django achieves pagination

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.