django分页相关类

本文介绍了Django中的Paginator类,包括构造函数、必传参数、可选参数、方法和属性。同时,讨论了Page类的方法和属性,如has_next、has_previous等。Paginator的InvalidPage异常也在文中提及。
摘要由CSDN通过智能技术生成

  主要参照了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.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相关的分页类
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值