分页器Link to this heading
Django 提供了一些类来帮助你管理分页数据 ——也就是说,数据被分割在几个页面上,并带有 “上一页/下一页” 的链接。这些类位于 django/core/paginator.py 中。
有关示例,请参阅 分页主题指南。
Paginator 类Link to this heading
- class Paginator(object_list, per_page, orphans=0, allow_empty_first_page=True, error_messages=None)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
Optional. 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, andorphans=3, there will be two pages; the first page with 10 items and the second (and last) page with 13 items.orphansdefaults to zero, which means pages are never combined and the last page may have one item.orphansshould be less than theper_pagevalue.
- Paginator.allow_empty_first_pageLink to this definition
Optional. Whether or not the first page is allowed to be empty. If
Falseandobject_listis empty, then anEmptyPageerror will be raised.
- Paginator.error_messagesLink to this definition
error_messages参数允许你覆盖分页器将引发的默认消息。传入一个字典,其中的键与你想要覆盖的错误消息相匹配。可用的错误消息键包括:invalid_page、min_page和no_results。例如,这里是默认的错误消息:
>>> from django.core.paginator import Paginator >>> paginator = Paginator([1, 2, 3], 2) >>> paginator.page(5) Traceback (most recent call last): ... EmptyPage: That page contains no results以下是一个自定义错误消息的示例:
>>> paginator = Paginator( ... [1, 2, 3], ... 2, ... error_messages={"no_results": "Page does not exist"}, ... ) >>> paginator.page(5) Traceback (most recent call last): ... EmptyPage: Page does not exist
方法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
返回一个带有给定的基于 1 的索引的
Page对象。如果number无法通过调用int()转换为整数,则引发PageNotAnInteger。如果给定的页数不存在,则引发EmptyPage。
- Paginator.get_elided_page_range(number, *, on_each_side=3, on_ends=2)Link to this definition
返回一个基于 1 的页码列表,类似于
Paginator.page_range,但在Paginator.num_pages很大时,可能会在当前页码的一侧或两侧添加省略号。每个当前页码两侧包括的页数由
on_each_side参数决定,默认为 3。在页码范围的开头和结尾包括的页数由
on_ends参数决定,默认为 2。例如,如果
on_each_side和on_ends的默认值为,当前页码为 10,总共有 50 页,那么页码范围将是[1, 2, '…', 7, 8, 9, 10, 11, 12, 13, '…', 49, 50]。这将导致当前页的左侧有页码 7、8 和 9,右侧有页码 11、12 和 13,开头有页码 1 和 2,结尾有页码 49 和 50。如果给定的页码不存在,则引发
InvalidPage异常。
属性Link to this heading
- Paginator.ELLIPSISLink to this definition
一个可翻译的字符串,用作
get_elided_page_range()返回的页面范围中省略的页码的替代物。默认值是'…'。
- Paginator.countLink to this definition
所有页面的对象总数。
- Paginator.num_pagesLink to this definition
总页数。
- Paginator.page_rangeLink to this definition
以 1 为基础的页码范围迭代器,例如产生
[1,2,3,4]。
AsyncPaginator classLink to this heading
- class AsyncPaginator(object_list, per_page, orphans=0, allow_empty_first_page=True, error_messages=None)Link to this definition
Asynchronous version of
Paginator.AsyncPaginatorhas the same attributes and signatures asPaginator, with the following exceptions:The attribute
Paginator.countis supported as an asynchronous methodAsyncPaginator.acount().The attribute
Paginator.num_pagesis supported as an asynchronous methodAsyncPaginator.anum_pages().The attribute
Paginator.page_rangeis supported as an asynchronous methodAsyncPaginator.apage_range().
AsyncPaginatorhas asynchronous versions of the same methods asPaginator, using anaprefix - for example, useawait async_paginator.aget_page(number)rather thanpaginator.get_page(number).
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对象。
AsyncPage classLink to this heading
- class AsyncPage(object_list, number, paginator)Link to this definition
Asynchronous version of
Page.AsyncPagehas the same attributes and signatures asPage, as well as asynchronous versions of all the same methods, using anaprefix - for example, useawait async_page.ahas_next()rather thanpage.has_next().AsyncPagehas the following additional method:- aget_object_list()Link to this definition
Returns
AsyncPage.object_listas a list. This method must be awaited beforeAsyncPagecan be treated as a sequence ofAsyncPage.object_list.
异常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 处理这两个异常。