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的更多相关文章

  1. django分页 Paginator

    分页功能是几乎所有的网站上都需要提供的功能,当你要展示的条目比较多时,必须进行分页,不但能减小数据库读取数据压力,也有利于用户浏览. Django又很贴心的为我们提供了一个Paginator分页工具, ...

  2. django 之Paginator

    Django自身提供了一些类来实现管理分页,数据被分在不同的页面中,并带有“上一页/下一页”标签.这个类叫做Pagination,其定义位于 django/core/paginator.py 中. p ...

  3. Django 之 Paginator 分页功能

    Django Paginator Django 分页官方文档  https://docs.djangoproject.com/en/1.10/topics/pagination/ 此分页方法没有限制显 ...

  4. Django 使用Paginator分页

    from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger subclass_s = models.subclas ...

  5. django 分页器 Paginator 基础操作

    基于下面这个分页器,说明常用的属性 from django.core.paginator import Paginator #导入Paginator类 from sign.models import ...

  6. Django中扩展Paginator实现分页

    Reference:https://my.oschina.net/kelvinfang/blog/134342 Django中已经实现了很多功能,基本上只要我们需要的功能,都能够找到相应的包.要在Dj ...

  7. Django的分页器(paginator)

    先导入模块: from django.core.paginator import Paginator, EmptyPage, PageNotAnInteger 分页器paginator 下面的所有方法 ...

  8. Django组件 之 分页器(paginator)

    --------------------------------------------------------------------------------路虽远,行则将至.  事虽难,做则必成. ...

  9. Django(十四)分页器(paginator)及自定义分页D

    http://www.mamicode.com/info-detail-1724597.html http://www.cnblogs.com/wupeiqi/articles/5246483.htm ...

随机推荐

  1. PREV-5_蓝桥杯_错误票据

    问题描述 某涉密单位下发了某种票据,并要在年终全部收回. 每张票据有唯一的ID号.全年所有票据的ID号是连续的,但ID的开始数码是随机选定的. 因为工作人员疏忽,在录入ID号的时候发生了一处错误,造成 ...

  2. 【git】之使用eclipse-git插件查看本地文件和远程文件区别

    选中文件右键 Team->Show Local History 在历史中右键 Compare Current with Local

  3. TweenMax 参考

    http://bbs.9ria.com/thread-214959-1-1.html TweenMax 可能是很多人都用的,包括我 但 是最近发现大量的运用就总会产生这样或那样的"怪事&qu ...

  4. 使用R语言-计算均值,方差等

    R语言对于数值计算很方便,最近用到了计算方差,标准差的功能,特记录. 数据准备 height <- c(6.00, 5.92, 5.58, 5.92) 1 计算均值 mean(height) [ ...

  5. Web jsp开发学习——Session使用

    先展示效果:              导包:    在servlet的doget里创建session      在head里显示session     接下来进行注销的命令    点击注销链接到这个 ...

  6. Java-Runoob-高级教程-实例-方法:11. Java 实例 – enum 和 switch 语句使用

    ylbtech-Java-Runoob-高级教程-实例-方法:11. Java 实例 – enum 和 switch 语句使用 1.返回顶部 1. Java 实例 - enum 和 switch 语句 ...

  7. 学习笔记之深度学习(Deep Learning)

    深度学习 - 维基百科,自由的百科全书 https://zh.wikipedia.org/wiki/%E6%B7%B1%E5%BA%A6%E5%AD%A6%E4%B9%A0 深度学习(deep lea ...

  8. Jmeter(三十四)Jmeter-Question之“Cookie获取”

    2018.4.27 还在做性能测试的过程中,唉,只能说坑很多. 无明确需求.无人手协调等问题,什么都需要自己去挖掘. 本次测试的工具选型依然是Jmeter,真实场景中遇到了这么个问题.可能解决办法有点 ...

  9. Jmeter(二十八)Jmeter-Question之“HTTPS请求”

    前面在Jmeter-Question中有提到若干问题,有时间呢,我也会进行继续编写随笔,梳理自己的知识,本篇呢,便来记Jmeter发送https请求的过程 内容大致与http://blog.csdn. ...

  10. RHEL7或CentOS7安装11.2.0.4 RAC碰到的问题

    RHEL7或CentOS7安装11.2.0.4 RAC碰到的问题 随着Linux 版本的普及,但Oracle数据库主流版本仍是11gR2, 的支持不很完美,在Linux 上安装会遇到几处问题,以此记录 ...