django分页相关类

来源:互联网 发布:淘宝小型高音收录放机 编辑:程序博客网 时间:2024/06/01 10:08

 主要参照了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 equalto orphans, then those items will be added to the previous page (whichbecomes the last page) instead of leaving the items on a page bythemselves. For example, with 23 items, per_page=10, andorphans=3, there will be two pages; the first page with 10 items andthe second (and last) page with 13 items.
allow_empty_first_page
Whether or not the first page is allowed to be empty. If False andobject_list is empty, then an EmptyPage error will be raised.

方法¶

Paginator.page(number)

根据索引number,返回一个’Page’对象,如果不存在,引起 InvalidPage异常

属性¶

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()

道理同上。

属性¶

Page.object_list

当前页对象列表

Page.number

当前页的索引

Page.paginator

和page相关的分页类