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