主要参照了django的官方文档
Paginator 类¶
构造函数:
Paginator(object_list, per_page, orphans=0, allow_empty_first_page=True)
必传参数¶
-
object_list
- 一个list,tuple,django的QuerySet,或者拥有``count()``和``__len__()``方法的 可分解对象。 per_page
- 每一页最大的对象个数。
可选参数¶
-
orphans
- The minimum number of items allowed on the last page, defaults to zero. Use this when you don’t want to have a last page with very few items. If the last page would normally have a number of items less than or equal to orphans, then those items will be added to the previous page (which becomes the last page) instead of leaving the items on a page by themselves. For example, with 23 items, per_page=10, and orphans=3, there will be two pages; the first page with 10 items and the second (and last) page with 13 items. allow_empty_first_page
- Whether or not the first page is allowed to be empty. If False and object_list is empty, then an EmptyPage error will be raised.
属性¶
Paginator.count
所有对象的总数, 尝试通过``object_list.count()``和``object_list.__len__()`` 取得
Paginator.num_pages
总共的页数
Paginator.page_range
页的范围,比如 [1, 2, 3, 4] 。
InvalidPage 异常¶
当页面不存在或者无效时,会引起``InvalidPage``异常,一般这个异常就够用,如果需要更 详细信息,请查阅官方文档。还有``PageNotAnInteger``,``EmptyPage``可用
Page 类¶
Page(object_list, number, paginator):
一般不需用户自己构造,通过`Paginator.page` 生成。
方法¶
Page.has_next()
如果下一页存在,返回True。
Page.has_previous()
如果前一页存在返回 True
Page.has_other_pages()
如果上一页面或者下一页存在,返回``True``
Page.next_page_number()
返回下一页的索引,这个函数比较傻(不管下一页是否存在,都是简单的+1)
Page.previous_page_number()
返回上一页的索引,其他同上
Page.start_index()
返回当前页,第一个对象的索引。
Page.end_index()
道理同上。