Django implements paging. django implements paging.

Source: Internet
Author: User

Django implements paging. django implements paging.

This example describes how to implement paging in django. Share it with you for your reference. The details are 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 winlog.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, 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:

{% For winlog in winloglist. object_list %} {winlog. date }}| {winlog. time }}< br/>{% endfor % }{% if winloglist. has_previous %} <a href = "? Page = {winloglist. previus_page_number }}" title = "next page"> previous page </a >{% endif %}{% for p in page_range %}{% ifequal p winloglist. number %} <span >{{ p }}</span >{% else %} <a href = "? Page = {p }}" title = "page {p}" >{{ p }}</a >{% endifequal %}{% endfor % }{ % if winloglist. has_next %} <a href = "? Page ={{ winloglist. next_page_number }}" title = "next page"> next page </a >{% endif %} <! -- {UserList. number} page {userList. paginator. num_pages} -->

Paginator object:

Paginator class:
Class Paginator (object_list, per_page, orphans = 0, allow_empty_first_page = True)

Required parameters:

Object_list: a list or tuples. The element is a django QuerySet or slice object that contains the count () or _ len _ () method.
Per_page: Maximum number of entries on a page.

Optional parameters:

Orphans: the minimum number of entries allowed on the last page. The default value is 0. When the number of entries on the last page is smaller than or equal to orphans, these entries are added to the previous page.
Allow_empty_first_page: whether the first page is blank. If it is set to False and object_list is empty, an EmptyPage exception is thrown.

Method:

Paginator. page (number): returns a Page object with the sequence number starting with 1. If the given page number does not exist, an InvalidPage exception is thrown.

Attribute:

Paginator. num_pages: total page number
Paginator. page_range: the range of the number of pages, starting with 1, for example, [1, 2, 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: if the number provided to page () is a valid number but no object exists on the page, this exception is thrown.

Page Object:

Class Page (object_list, number, paginator ):
Generally, Pages are not created manually. You can use Paginator. page ().

Method:

Page. has_next (): returns True if the next Page exists.
Page. has_previous (): returns True if the previous Page exists.
Page. has_other_pages (): returns True if a previous or next Page exists.
Page. next_page_number (): return the Page number of the next Page. Whether or not the next page exists.
Page. previus_page_number (): return the Page number of the previous Page. Returns whether or not the previous page exists.
Page. start_index (): returns the sequence number of the first object on the current page. The sequence number starts with 1. for example, if you divide a list of five objects into two objects on each page, start_index () on the second page returns 3.
Page. end_index (): returns the sequence number of the most objects on the current Page.

Attribute:

Page. object_list: all objects on the current Page
Page. number: the Page number of the current Page, starting with 1
Page. paginator: The Pageinator object related to the Page.

I hope this article will help you with Python programming.

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.