The examples in this article describe how Django implements paging. Share to everyone for your reference. Specific as follows:
The Python code is as follows:
#!/usr/bin/env python#-*-coding:utf-8-*-# Create your views here.from django.shortcuts import Render_to_responsefrom W Inlog.log_dj.models Import winlogfrom django.core.paginator import Paginator,invalidpage,emptypage, Pagenotanintegerdef Index (Request): After_range_num = 5 before_range_num = 4 try: page=int (Request. Get.get (' page ', ' 1 ')) if page < 1: page=1 except valueerror: page=1 winlog_list = Winlog.objects.all (). order_by ('-id ') paginator = Paginator (Winlog_list, ten) try: winloglist = Paginator.page (page) except (Emptypage,invalidpage,pagenotaninteger): winloglist = paginator.page (1) if page >= after_range_num: page_range = Paginator.page_range[page-after_range_num:page+before_range_ Num] Else: page_range = paginator.page_range[0:int (page) +before_range_num] return render_to_ Response (' log_dj/index.html ', locals ())
The HTML page is as follows:
{% for winlog in winloglist.object_list%}
{% ENDFOR%} {% if winloglist.has_previous%} prev {% endif%}{% for P in Page_range%}{% ifequal p winloglist.number%}{{p}}{% else%}{{p}}{% endifequal%}{% endfor%} {% if Winloglist.has_next%} Next page {% endif%}
Paginator object:
Class Paginator:
Class Paginator (Object_list,per_page,orphans=0,allow_empty_first_page=true)
Parameters that must be supplied:
Object_list: A list or tuple in which the element is a Django Queryset or a sliced object that contains the count () or __len__ () method.
Per_page: Contains the maximum number of entries in a page.
Optional Parameters:
Orphans: The minimum number of entries in the last page, the default is 0. When the number of last page items is less than or equal to orphans, these entries are added to the previous page of this page.
Allow_empty_first_page: Whether or not the first page is allowed to be empty. If set to False and Object_list is empty, the Emptypage exception is thrown.
Method:
Paginator.page (number): Returns a Page object that starts at 1. Throws a Invalidpage exception if the given page number does not exist.
Property:
Paginator.num_pages: Page Total pages
Paginator.page_range: The range of pages, starting at 1, such as [1,2,3,4].
Invalidpage Exception:
If the requested page is invalid or there is no object in the page, the page () throws an Invalidpage exception.
Pagenotaninterger: The exception is thrown when the number supplied to the page () is not an integer.
Emptypage: When the number provided to page () is a valid number, but the exception is thrown when no object exists on the page.
Page object:
Class Page (Object_list,number,paginator):
Pages are not created manually, and Paginator.page () can be used.
Method:
Page.has_next (): Returns True if there is a next page
Page.has_previous (): Returns True if there is a previous page
Page.has_other_pages (): If there is a previous page or the next page returns True
Page.next_page_number (): Returns the page number of the next page. Returns regardless of whether the next page exists.
Page.previous_page_number (): Returns the page number of the previous page. Returns regardless of whether the previous page exists.
Page.start_index (): Returns the ordinal of the first object in the current page, starting at 1. For example, if you divide a list of 5 objects into 2 objects per page, Start_index () on the second page returns 3.
Page.end_index (): Returns the ordinal of the most object in the current page.
Property:
Page.object_list: All objects in the current page
Page.number: page number of the current screen, starting at 1
Page.paginator: page-related Pageinator object.
Hopefully this article will help you with Python programming.