程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Django paging method

編輯:Python

The example in this article describes how Django implements paging.Share it with everyone 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 = 5before_range_num = 4try:page=int(request.GET.get('page','1'))if page < 1:page=1except ValueError:page=1winlog_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 }} 
{% endfor %}{% if winloglist.has_previous %}previous page {% 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)

Required parameters:

object_list: A list or tuple whose elements are django QuerySets or sliceable objects with 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.

Attribute:

Paginator.num_pages: The total number of pages Paginator.page_range: The range of the number of pages, starting from 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, Pages are not created manually, you can use Paginator.page().

Method:

Page.has_next(): Return True if there is a next page Page.has_previous(): Return True if there is a previous page Page.has_other_pages(): Return True if there is a previous or next 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.

Attribute:

Page.object_list: All objects in the current page Page.number: The page number of the current page, starting from 1 Page.paginator: The page-related Pageinator object.

I hope this article will help you in your Python programming.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved