Django provides a class Paginator dedicated to the management and processing of paging data, so we use the first imported before the corresponding class, in addition, here we also imported to be used to deal with exceptions to the two classes EmptyPage and PageNotAnInteger:
from import Paginator, EmptyPage, PageNotAnInteger
Then write the view function test.
def test(request): course_list=() # Generate pagintor objects, define 10 records to be displayed per page paginator=Paginator(course_list,10) # Get the current page number, default is 1. page=("page",1) # Convert the current page number to an integer type currentPage=int(page) try: video_list=(page)# Get the current page number of the record except PageNotAnInteger: video_list=(1)# If the page number entered by the user is not an integer, the contents of page 1 are displayed. except EmptyPage: video_list=(paginator.num_pages)# If the page number entered by the user is not an integer, the contents of page 1 are displayed. return render(request, "", locals())
Next we complete the logic for the front-end page, where the file defined is.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Here's the title</title> </head> <body> <div class="manageContent"> {#Data display div--start#} {% for video in video_list%} <div class="content9"> <div class="videomsg"> <img src="/static/images/" alt="" /> <span >{{ }}</span> </div> <span class="courseTime" >{{ }}</span> <span class="sourse" >{{ }}</span> <span class="status" >{{ }}</span> <div class="operate"> <span style="cursor:pointer;" onclick="ShowUpDiv('upfileDiv','fade1','{{ }}','{{ }}')">upload</span> <span style="cursor:pointer;" onclick="ShowEditDiv('editDiv','editfade','{{ }}','{{ }}','{{ }}','{{ }}','{{ }}')" >modifications</span> <span style="cursor:pointer;" onclick="Showdetail('{{ }}')" >particulars</span> </div> <div style="display: none ;overflow:scroll;overflow-x:hidden"> </div> </div> {% endfor %} {#Data display div--end#} {# Show paging navigation bar - start#} <div class="kkk"> <ul class="pagination" > {#Previous button to start#} {# If the current page has a previous page #} {% if video_list.has_previous %} {# The previous button on the current page works fine #} <li class="previous"><a href="/backstage/test/?page={{ video_list.previous_page_number }}" >preceding page</a></li> {% else %} {# The previous page button is not available if the previous page does not exist on the current page #} <li class="previous disabled"><a href="#" >Previous</a></li> {% endif %} {#Previous button ends#} {# Start of page #} {% for num in paginator.page_range %} {% if num == currentPage %} <li class="liactive"><a class="selected" href="/backstage/test/?page={{ num }}" >{{ num }}</a></li> {% else %} <li class="itemli"><a href="/backstage/test/?page={{ num }}" >{{ num }}</a></li> {% endif %} {% endfor %} {#End of page #} {# Next button to start #} {% if video_list.has_next %} <li class="next"><a href="/backstage/test/?page={{ video_list.next_page_number }}" >next page</a></li> {% else %} <li class="next disabled"><a href="#" >Next</a></li> {% endif %} {# End of next page button #} </ul> </div> {#Display paging navigation bar--end#} </div> </body> </html>
At this point, the logic of the paging display is complete. More examples of Paginator syntax are listed below:
from import Paginator objects = ['john','paul','george','ringo','lucy','meiry','checy','wind','flow','rain']<br> p = Paginator(objects,3) # 3 pieces of data to a page, instantiate the paging object print # 10 The object has a total of 10 elements print p.num_pages # 4 Objects can be divided into 4 pages print p.page_range # xrange(1, 5) Iterable range of object pages page1 = (1) # Take the first pagination object of the object print page1.object_list # List of elements of the first paging object ['john', 'paul', 'george'] print # Current page value of the first paging object 1 page2 = (2) # Take the second paging object of the object print page2.object_list # List of elements of the second paging object ['ringo', 'lucy', 'meiry'] print # Current page number value of the second pagination object 2 print page1.has_previous() # Whether the first paging object has a previous page False print page1.has_other_pages() # Whether the first paging object has other pages True print page2.has_previous() # Whether the second paging object has a previous page True print page2.has_next() # Whether the second paging object has a next page True print page2.next_page_number() # Value of the next page code of the second paging object 3 print page2.previous_page_number() # Previous page code value of the second paging object 1 print page2.start_index() # Start indexing elements of the second paging object 4 print page2.end_index() # (prefix indicating ordinal number, e.g. first, number two etc)2End-of-element index for paging objects 6
This is the whole content of this article.