django之paginator
class Paginator(object):#分页器 def __init__(self, object_list, per_page, orphans=0,
allow_empty_first_page=True):
self.object_list = object_list#要分页的数据列表
self.per_page = int(per_page)#每页多少条
self.orphans = int(orphans)
self.allow_empty_first_page = allow_empty_first_page
self._num_pages = self._count = None#总页数、总条数 def validate_number(self, number):#校验页数是否合法
"""
Validates the given 1-based page number.
"""
try:
number = int(number)
except (TypeError, ValueError):
raise PageNotAnInteger('That page number is not an integer')
if number < 1:
raise EmptyPage('That page number is less than 1')
if number > self.num_pages:
if number == 1 and self.allow_empty_first_page:
pass
else:
raise EmptyPage('That page contains no results')
return number def page(self, number):#返回指定的页
"""
Returns a Page object for the given 1-based page number.
"""
number = self.validate_number(number)
bottom = (number - 1) * self.per_page
top = bottom + self.per_page
if top + self.orphans >= self.count:
top = self.count
return self._get_page(self.object_list[bottom:top], number, self) def _get_page(self, *args, **kwargs):
"""
Returns an instance of a single page. This hook can be used by subclasses to use an alternative to the
standard :cls:`Page` object.
"""
return Page(*args, **kwargs) def _get_count(self):
"""
Returns the total number of objects, across all pages.
"""
if self._count is None:
try:
self._count = self.object_list.count()
except (AttributeError, TypeError):
# AttributeError if object_list has no count() method.
# TypeError if object_list.count() requires arguments
# (i.e. is of type list).
self._count = len(self.object_list)
return self._count
count = property(_get_count) def _get_num_pages(self):
"""
Returns the total number of pages.
"""
if self._num_pages is None:
if self.count == 0 and not self.allow_empty_first_page:
self._num_pages = 0
else:
hits = max(1, self.count - self.orphans)
self._num_pages = int(ceil(hits / float(self.per_page)))
return self._num_pages
num_pages = property(_get_num_pages) def _get_page_range(self):#获取页范围
"""
Returns a 1-based range of pages for iterating through within
a template for loop.
"""
return six.moves.range(1, self.num_pages + 1)
page_range = property(_get_page_range) QuerySetPaginator = Paginator # For backwards-compatibility. class Page(collections.Sequence):#页类继承系列 def __init__(self, object_list, number, paginator):
self.object_list = object_list
self.number = number
self.paginator = paginator#指向分页器 def __repr__(self):
return '<Page %s of %s>' % (self.number, self.paginator.num_pages) def __len__(self):
return len(self.object_list) def __getitem__(self, index):#取得页中的指定item
if not isinstance(index, (slice,) + six.integer_types):
raise TypeError
# The object_list is converted to a list so that if it was a QuerySet
# it won't be a database hit per __getitem__.
if not isinstance(self.object_list, list):
self.object_list = list(self.object_list)
return self.object_list[index] def has_next(self):#是否有下一页
return self.number < self.paginator.num_pages def has_previous(self):
return self.number > 1 def has_other_pages(self):
return self.has_previous() or self.has_next() def next_page_number(self):
return self.paginator.validate_number(self.number + 1) def previous_page_number(self):
return self.paginator.validate_number(self.number - 1) def start_index(self):
"""
Returns the 1-based index of the first object on this page,
relative to total objects in the paginator.
"""
# Special case, return zero if no items.
if self.paginator.count == 0:
return 0
return (self.paginator.per_page * (self.number - 1)) + 1 def end_index(self):
"""
Returns the 1-based index of the last object on this page,
relative to total objects found (hits).
"""
# Special case for the last page because there can be orphans.
if self.number == self.paginator.num_pages:
return self.paginator.count
return self.number * self.paginator.per_page
django之paginator的更多相关文章
- django分页 Paginator
分页功能是几乎所有的网站上都需要提供的功能,当你要展示的条目比较多时,必须进行分页,不但能减小数据库读取数据压力,也有利于用户浏览. Django又很贴心的为我们提供了一个Paginator分页工具, ...
- django 之Paginator
Django自身提供了一些类来实现管理分页,数据被分在不同的页面中,并带有“上一页/下一页”标签.这个类叫做Pagination,其定义位于 django/core/paginator.py 中. p ...
- Django 之 Paginator 分页功能
Django Paginator Django 分页官方文档 https://docs.djangoproject.com/en/1.10/topics/pagination/ 此分页方法没有限制显 ...
- Django 使用Paginator分页
from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger subclass_s = models.subclas ...
- django 分页器 Paginator 基础操作
基于下面这个分页器,说明常用的属性 from django.core.paginator import Paginator #导入Paginator类 from sign.models import ...
- Django中扩展Paginator实现分页
Reference:https://my.oschina.net/kelvinfang/blog/134342 Django中已经实现了很多功能,基本上只要我们需要的功能,都能够找到相应的包.要在Dj ...
- Django的分页器(paginator)
先导入模块: from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger 分页器paginator 下面的所有方法 ...
- Django组件 之 分页器(paginator)
--------------------------------------------------------------------------------路虽远,行则将至. 事虽难,做则必成. ...
- Django(十四)分页器(paginator)及自定义分页D
http://www.mamicode.com/info-detail-1724597.html http://www.cnblogs.com/wupeiqi/articles/5246483.htm ...
随机推荐
- [蓝桥杯]ALGO-90.算法训练_出现次数最多的整数
问题描述 编写一个程序,读入一组整数,这组整数是按照从小到大的顺序排列的,它们的个数N也是由用户输入的,最多不会超过20.然后程序将对这个数组进行统计,把出现次数最多的那个数组元素值打印出来.如果有两 ...
- How HipChat Stores And Indexes Billions Of Messages Using ElasticSearch And Redis[转]
This article is from an interview with Zuhaib Siddique, a production engineer at HipChat, makers of ...
- CLion使用OpenCV(Ubuntu 18.04)
项目结构: 设置CMainLists.txt文件中的内容: cmake_minimum_required(VERSION 3.13) project(cv_test) ) find_package(O ...
- unity3d动态创建一个文本
2D文本需要Canvas和EventSystem,最好使用Editor来添加: 动态显示一个文本,采用3D Text的方式: GameObject text = new GameObject(); t ...
- DB通用类:Access通用类
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- Java——ikanalyzer分词·只用自定义词库
需要包:IKAnalyzer2012_FF_hf1.jarlucene-core-5.5.4.jar需要文件: IKAnalyzer.cfg.xmlext.dicstopword.dic 整理好的下载 ...
- Hive与HBase集成进行数据分析
我们把hive的安装包上传的节点3来 解压 现在我们还是老规矩通过notopad++来连接我们的虚拟机来配置文件,把下面这两个文件重命名一下 修改这个文件 对hive-env.sh我们修改这里 下面我 ...
- CS229 6.2 Neurons Networks Backpropagation Algorithm
今天得主题是BP算法.大规模的神经网络可以使用batch gradient descent算法求解,也可以使用 stochastic gradient descent 算法,求解的关键问题在于求得每层 ...
- echarts.js应用之map
最近项目中用到了echarts.js中的map,我画了一个简版的案例,如下所示: 效果图: 主要代码如下: <!DOCTYPE html> <html lang="en&q ...
- 导入sql文件报错:1071 Specified key was too long; max key length is 767 bytes
ref: https://stackoverflow.com/questions/1814532/1071-specified-key-was-too-long-max-key-length-is-7 ...