分页器Link to this heading
Django 提供了一些类来帮助你管理分页数据 ——也就是说,数据被分割在几个页面上,并带有 “上一页/下一页” 的链接。这些类位于 django/core/paginator.py 中。
For examples, see the Pagination topic guide.
Paginator 类Link to this heading
- class Paginator(object_list, per_page, orphans=0, allow_empty_first_page=True)Link to this definition
当使用
len()或直接迭代时,分页器的作用就像一个Page的序列。
- Paginator.object_listLink to this definition
必要的。一个列表、元组、
QuerySet或其他具有count()或__len__()方法的可切片对象。为了实现一致的分页,QuerySet应该是有序的,例如使用order_by()子句或使用模型上的默认ordering。
- Paginator.per_pageLink to this definition
必要的。一个页面中包含的最大项目数,不包括 orphans(参见下面的
orphans可选参数)。
- Paginator.orphansLink to this definition
可选的。当你不希望最后一页的项目数量很少时,使用这个选项。如果最后一页的项目数量通常小于或等于
orphans,那么这些项目将被添加到前一页(成为最后一页),而不是让这些项目单独留在一页上。例如,如果有 23 个条目,per_page=10,orphans=3,则会有两页;第一页有 10 个条目,第二页(也是最后一页)有 13 个条目。orphans默认为 0,这意味着页面永远不会合并,最后一页可能只有一个项目。
- Paginator.allow_empty_first_pageLink to this definition
可选的。是否允许第一页为空。 如果
False并且object_list是空的,则会出现EmptyPage错误。
方法Link to this heading
- Paginator.get_page(number)Link to this definition
返回一个给定的基于 1 索引的
Page对象,同时处理超出范围和无效的页码。如果页数不是数字,它返回第一页。如果页码为负数或大于页数,则返回最后一页。
只有当你指定了
Paginator(..., allow_empty_first_page=False)并且object_list为空时,才会引发EmptyPage异常。
- Paginator.page(number)Link to this definition
Returns a
Pageobject with the given 1-based index. RaisesPageNotAnIntegerif thenumbercannot be converted to an integer by callingint(). RaisesInvalidPageif the given page number doesn't exist.
- Paginator.get_elided_page_range(number, *, on_each_side=3, on_ends=2)Link to this definition
-
Returns a 1-based list of page numbers similar to
Paginator.page_range, but may add an ellipsis to either or both sides of the current page number whenPaginator.num_pagesis large.The number of pages to include on each side of the current page number is determined by the
on_each_sideargument which defaults to 3.The number of pages to include at the beginning and end of page range is determined by the
on_endsargument which defaults to 2.For example, with the default values for
on_each_sideandon_ends, if the current page is 10 and there are 50 pages, the page range will be[1, 2, '…', 7, 8, 9, 10, 11, 12, 13, '…', 49, 50]. This will result in pages 7, 8, and 9 to the left of and 11, 12, and 13 to the right of the current page as well as pages 1 and 2 at the start and 49 and 50 at the end.Raises
InvalidPageif the given page number doesn't exist.
属性Link to this heading
- Paginator.ELLIPSISLink to this definition
-
A translatable string used as a substitute for elided page numbers in the page range returned by
get_elided_page_range(). Default is'…'.
- Paginator.countLink to this definition
所有页面的对象总数。
- Paginator.num_pagesLink to this definition
总页数。
- Paginator.page_rangeLink to this definition
以 1 为基础的页码范围迭代器,例如产生
[1,2,3,4]。
Page 类Link to this heading
你通常不会手工构建 Page 对象 —— 你将通过迭代 Paginator,或使用 Paginator.page() 获得它们。
- class Page(object_list, number, paginator)Link to this definition
当使用
len()或直接迭代时,一个页面就像一个Page.object_list的序列。
方法Link to this heading
- Page.has_next()Link to this definition
如果有下一页,返回
True。
- Page.has_previous()Link to this definition
如果有上一页,返回
True。
- Page.has_other_pages()Link to this definition
如果有下一页 或 上一页,返回
True。
- Page.next_page_number()Link to this definition
返回下一页的页码。如果下一页不存在,则引发
InvalidPage。
- Page.previous_page_number()Link to this definition
返回上一页的页码。如果上一页不存在,则引发
InvalidPage。
- Page.start_index()Link to this definition
返回页面上第一个对象,相对于分页器列表中所有对象的基于 1 的索引。例如,当对一个有 5 个对象的列表进行分页时,每页有 2 个对象,第二页的
start_index()将返回3。
- Page.end_index()Link to this definition
返回页面上最后一个对象相对于分页器列表中所有对象的基于 1 的索引。例如,当对一个有 5 个对象的列表进行分页时,每页有 2 个对象,第二页的
end_index()将返回4。
属性Link to this heading
- Page.object_listLink to this definition
此页上的对象列表。
- Page.numberLink to this definition
此页的基于 1 的页码。
- Page.paginatorLink to this definition
关联的
Paginator对象。
异常Link to this heading
- exception InvalidPageLink to this definition
当分页器被传递一个无效的页码时引发异常的基类。
Paginator.page() 方法在请求的页面无效(即不是整数)或不包含任何对象时引发异常。一般来说,只要捕获 InvalidPage 异常就够了,但如果你想要更细化,你可以捕获以下任何一种异常。
- exception PageNotAnIntegerLink to this definition
当
page()的值不是整数时发生该事件。
- exception EmptyPageLink to this definition
当
page()被赋予一个有效的值,但该页面上没有对象存在时,引发该异常。
这两个异常都是 InvalidPage 的子类,所以你可以用 except InvalidPage 处理这两个异常。