The example in this article describes how django implements paging. Share it with everyone for your reference. details as follows:
The Python code is as follows:
copy#!/usr/bin/env python # -*- coding: utf-8 -*- # Create your views here. from django.shortcuts import render_to_response from winlog.log_dj.models import Winlog from django.core.paginator import Paginator,InvalidPage,EmptyPage,PageNotAnInteger def 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, 10) 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:
copy{% for winlog in winloglist.object_list %} {{ winlog.date }}|{{ winlog.time }} <br /> {% endfor %} {% if winloglist.has_previous %} <a href="?page={{ winloglist.previous_page_number }}" title="next page">previous page</a>& nbsp; {% endif %} {% for p in page_range %} {% ifequal p winloglist.number %} <span>{{p}}</span> {% else %} <a href="?page={{p}}" title="the first{{p}}Page">{{p}}</a> {% endifequal %} {% endfor %} {% if winloglist.has_next %} <a href="?page={{ winloglist.next_page_number }}" title="next page">next page</a> ; {% endif %} <!-- the first {{ userList.number }} Page common {{ userList.paginator.num_pages }} Page-->
Paginator object:
class Paginator: class Paginator(object_list,per_page,orphans=0,allow_empty_first_page=True)
Required parameters:
object_list: A list or tuple whose elements are django QuerySet s or sliceable objects containing count() or __len__() methods. per_page: The maximum number of entries contained in a page.
Optional parameters:
orphans: The minimum number of entries allowed in the last page, the default is 0. When the number of entries in the last page is less than or equal to orphans, these entries are added to the previous page of this page. allow_empty_first_page: Whether to allow the first page to be empty. If set to False and object_list is empty, an EmptyPage exception will be thrown.
method:
Paginator.page(number): Returns a Page object, the serial number starts from 1. If the given page number does not exist, an InvalidPage exception is thrown.
Attributes:
Paginator.num_pages: total number of pages Paginator.page_range: The range of page numbers, starting with 1, such as [1,2,3,4].
InvalidPage exception:
If the requested page is invalid or there are no objects in the page, page() throws an InvalidPage exception. PageNotAnInterger: This exception is thrown when the number provided to page() is not an integer. EmptyPage: This exception is thrown when the number provided to page() is a valid number, but no objects exist on the page.
Page object:
class Page(object_list,number,paginator): Generally do not create Pages manually, you can use Paginator.page().
method:
Page.has_next(): Return True if there is a next page Page.has_previous(): Returns True if there is a previous page Page.has_other_pages(): Return True if there is previous or next page Page.next_page_number(): Returns the page number of the next page. Returns whether or not the next page exists. Page.previous_page_number(): Returns the page number of the previous page. Returns whether or not the previous page exists. Page.start_index(): Returns the serial number of the first object in the current page, and the serial number starts at 1. For example, if a list containing 5 objects is divided into 2 objects per page, the start_index() of the second page returns 3. Page.end_index(): Returns the serial number of the last object in the current page.
Attributes:
Page.object_list: All objects in the current page Page.number: The page number of the current page, starting at 1 Page.paginator: Page-related Pageinator object.
I hope this article will help you in your Python programming.