Abstract
Django提供了一系列类来帮助你管理分页数据。这些类位于
django/core/paginator.py
测试
root@pts/0 # python
Python 3.5.2 (default, Aug 8 2017, 15:35:13)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from django.core.paginator import Paginator
## 作为测试分页的对象
>>> objects = ['post1', 'post2', 'post3', 'post4', 'post5', 'post6', 'post7']
## 期望按照每页三个对象来实现分页
>>> paginator = Paginator(objects, 3)
## 输入paginator. 然后连续按两次 tab 键,展示 paginator 对应的方法属性等
>>> paginator.
paginator.__class__( paginator.__le__( paginator.__weakref__
paginator.__delattr__( paginator.__lt__( paginator._check_object_list_is_ordered(
paginator.__dict__ paginator.__module__ paginator._get_page(
paginator.__dir__( paginator.__ne__( paginator.allow_empty_first_page
paginator.__doc__ paginator.__new__( paginator.count
paginator.__eq__( paginator.__reduce__( paginator.num_pages
paginator.__format__( paginator.__reduce_ex__( paginator.object_list
paginator.__ge__( paginator.__repr__( paginator.orphans
paginator.__getattribute__( paginator.__setattr__( paginator.page(
paginator.__gt__( paginator.__sizeof__( paginator.page_range
paginator.__hash__( paginator.__str__( paginator.per_page
paginator.__init__( paginator.__subclasshook__( paginator.validate_number(
## 要分页的总对象数
>>> paginator.count
7
>>> paginator.object_list
['post1', 'post2', 'post3', 'post4', 'post5', 'post6', 'post7']
## 总共可以分页数,也就是总共可以分成多少页
>>> paginator.num_pages
3
## 页码取值范围
>>> paginator.page_range
range(1, 4)
## 每页几个对象
>>> paginator.per_page
3
## 取第2页, 或者第N页`paginator.page(N)`
>>> page2 = paginator.page(2)
## page2 的属性和方法
>>> page2.
page2.__abstractmethods__ page2.__le__( page2._abc_cache
page2.__class__( page2.__len__( page2._abc_negative_cache
page2.__contains__( page2.__lt__( page2._abc_negative_cache_version
page2.__delattr__( page2.__module__ page2._abc_registry
page2.__dict__ page2.__ne__( page2.count(
page2.__dir__( page2.__new__( page2.end_index(
page2.__doc__ page2.__reduce__( page2.has_next(
page2.__eq__( page2.__reduce_ex__( page2.has_other_pages(
page2.__format__( page2.__repr__( page2.has_previous(
page2.__ge__( page2.__reversed__( page2.index(
page2.__getattribute__( page2.__setattr__( page2.next_page_number(
page2.__getitem__( page2.__sizeof__( page2.number
page2.__gt__( page2.__slots__ page2.object_list
page2.__hash__( page2.__str__( page2.paginator
page2.__init__( page2.__subclasshook__( page2.previous_page_number(
page2.__iter__( page2.__weakref__ page2.start_index(
## 第2页含有的对象列表
>>> page2.object_list
['post4', 'post5', 'post6']
## 从最上面我们知道,总共有7个对象,每页三个对象,我们总共分成3个页面,第二个页面在中间
## 所有第二页有next和previous, 并且可以得到Next 和 previous 的页码值
>>> page2.has_next()
True
>>> page2.next_page_number()
3
>>> page2.has_previous()
True
>>> page2.previous_page_number()
1
>>> page2.has_other_pages()
True
## 第二页开始和结束的对象索引
>>> page2.start_index()
4
>>> page2.end_index()
6
>>>
## 针对该测试例子。我们知道0和4页肯定是不存在, 所以出现 emptypage 异常
## 所以一般页面不存在或者页面无效之类的异常。我们都会让其跳转到第一页
>>> paginator.page(0)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/root/.pyenv/versions/blog/lib/python3.5/site-packages/django/core/paginator.py", line 57, in page
number = self.validate_number(number)
File "/root/.pyenv/versions/blog/lib/python3.5/site-packages/django/core/paginator.py", line 45, in validate_number
raise EmptyPage(_('That page number is less than 1'))
django.core.paginator.EmptyPage: <exception str() failed>
>>> paginator.page(4)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/root/.pyenv/versions/blog/lib/python3.5/site-packages/django/core/paginator.py", line 57, in page
number = self.validate_number(number)
File "/root/.pyenv/versions/blog/lib/python3.5/site-packages/django/core/paginator.py", line 50, in validate_number
raise EmptyPage(_('That page contains no results'))
django.core.paginator.EmptyPage: <exception str() failed>
>>>