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 ...
随机推荐
- 数据仓库专题19-数据建模语言Information Engineering - IE模型(转载)
Information Engineering采用Crow's Foot表示法(也有叫做James Martin表示法的),中文翻译中对使用了Crow's Foot表示法的模型也有笼统的称做鸭掌模型的 ...
- Eclipse 安装阿里巴巴代码规范插件
好像是要求jdk1.8+ ,1.8以下的没试过 第一步: 选择 Install New Software 第二步: https://p3c.alibaba.com/plugin/eclipse/up ...
- C++进阶--类的继承
//############################################################################ /* * 公有,保护,私有继承 */ cl ...
- pyqt信号和槽传递额外参数
转载:fengyu09 环境:python2.7.8 —— pyqt 4.11.1 使用Pyqt编程过程中,经常会遇到给槽函数传递额外参数的情况.但是信号-槽机制只是指定信号如何连接到槽,信号定义的参 ...
- sass之为什么要使用预处理器
使用预处理器主要目的就是编写出可读性更好.更易于维护的css. 以sass为例,sass中提供了@import可以在sass文件中导入其他sass文件,或在选择器中按需导入所需要的某个属性样式: @i ...
- docker安装testlink
testlink 镜像 https://hub.docker.com/r/bitnami/testlink ```#shell 下载镜像 docker pull bitnami/testlink
- google最新的书签导入导出
1.google浏览器地址栏最右边,自定义及控制--->书签----->书签管理器 2. 右上角,有整理图标, 3.点击按钮即可导入导出书签
- 使用CacheCloud管理Redis实例
转载来源:http://www.ywnds.com/?p=10610 一.CacheCloud是什么? 最近在使用CacheCloud管理Redis,所以简单说一下,这里主要说一下我碰到的问题.Cac ...
- 第6章 静态路由和动态路由(4)_OSPF动态路由协议
6. OSPF动态路由协议 6.1 OSPF协议(Open Shortest Path First,OSPF开放式最短路径优先协议) (1)通过路由器之间通告链路的状态来建立链路状态数据库,网络中所有 ...
- (转)C# WebApi 身份认证解决方案:Basic基础认证
原文地址:http://www.cnblogs.com/landeanfen/p/5287064.html 阅读目录 一.为什么需要身份认证 二.Basic基础认证的原理解析 1.常见的认证方式 2. ...