ページ分割 (Paginator)¶
Django はページ分割されたデータを管理するのに役立ついくつかのクラスを提供しています。これらのクラスは django/core/paginator.py にあります。
例については ページ分割 (Pagination) のトピックガイド を参照してください。
Paginator class¶
- class Paginator(object_list, per_page, orphans=0, allow_empty_first_page=True, error_messages=None)[ソース]¶
Paginator(ページネーター)は
len()の使用時、または直接イテレートしたときはPageのシーケンスのように動作します。
- Paginator.object_list¶
必須です。リスト、タプル、
QuerySet、またはcount()または__len__()メソッドを持つその他のスライス可能なオブジェクト。一貫したページ分割のためには、QuerySetは順序付けされるべきです。たとえば、order_by()句を使用するか、モデル上のデフォルトのorderingで行います。巨大な
QuerySetのページ分割によるパフォーマンス上の問題非常に多数のアイテムを持つ
QuerySetを使用した場合、数の大きいページをリクエストしたときに、データベースによっては速度が低下する場合があります。これは、OFFSET数を数えるために必要なLIMIT/OFFSETクエリの処理時間が、ページ数が増えるにつれて長くなってしまうためです。
- Paginator.orphans¶
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.Deprecated since version 6.0: Support for the
orphansargument being larger than or equal to theper_pageargument is deprecated.
- Paginator.allow_empty_first_page¶
Optional. Whether or not the first page is allowed to be empty. If
Falseandobject_listis empty, then anEmptyPageerror will be raised.
- Paginator.error_messages¶
error_messages引数を指定すると、paginator がデフォルトで返すメッセージを上書きできます。オーバーライドしたいエラーメッセージにマッチするキーを持つ辞書を渡します。使用可能なエラーメッセージのキーは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
メソッド¶
- Paginator.get_page(number)[ソース]¶
1から始まるインデックスをもつ
Pageオブジェクトを返します。このオブジェクトは、範囲外のページ数や無効なページ数もハンドリングします。与えられた値が数字でなかった場合は、最初のページを返します。ページ数が負の数や全体のページ数より大きかった場合は、最後のページを返します。
Paginator(..., allow_empty_first_page=False)を指定し、object_listが空の場合にのみEmptyPage例外を発生させます。
- Paginator.page(number)[ソース]¶
1から始まるインデックスを持つ
Pageオブジェクトを返します。数値numberがint()を呼び出して整数に変換できない場合、PageNotAnIntegerを発生させます。指定されたページ番号が存在しない場合、EmptyPageを発生させます。
- Paginator.get_elided_page_range(number, *, on_each_side=3, on_ends=2)[ソース]¶
Paginator.page_rangeに似た1から始まるページ番号のリストを返しますが、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を発生させます。
属性¶
- Paginator.ELLIPSIS¶
get_elided_page_range()によって返されるページ範囲において、ページ番号の代わりに使用される翻訳可能な文字列です。デフォルトは'...'です。
AsyncPaginator class¶
- class AsyncPaginator(object_list, per_page, orphans=0, allow_empty_first_page=True, error_messages=None)[ソース]¶
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 クラス¶
通常は Page オブジェクトを手動で構築することはありません。 Paginator をイテレートするか、 Paginator.page() を使ってオブジェクトを取得します。
- class Page(object_list, number, paginator)[ソース]¶
1つのページは、
len()を使ったり直接イテレーションした時、Page.object_listのシーケンスのように動作します。
メソッド¶
- Page.next_page_number()[ソース]¶
次のページ数を返します。次のページが存在しないときは
InvalidPage例外を起こします。
- Page.previous_page_number()[ソース]¶
前のページ数を返します。前のページが存在しないときは
InvalidPage例外を起こします。
- Page.start_index()[ソース]¶
ページ上の最初のオブジェクトに対する、1から始まるインデックスを返します。これは、ページネータのリストに含まれる全オブジェクトに対するインデックスです。たとえば、5個のオブジェクトのリストを各ページ2オブジェクトでページ分割している場合、2ページ目の
start_index()は3を返すでしょう。
- Page.end_index()[ソース]¶
ページ上の最後のオブジェクトに対する、1から始まるインデックスを返します。これは、ページネータのリストに含まれる全オブジェクトに対するインデックスです。たとえば、5個のオブジェクトのリストを各ページ2オブジェクトでページ分割している場合、2ページ目の
end_index()は4を返すでしょう。
属性¶
- Page.object_list¶
当該のページに含まれるオブジェクトのリストです。
- Page.number¶
1から数えた現在のページのページ数です。
AsyncPage class¶
- class AsyncPage(object_list, number, paginator)[ソース]¶
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()¶
Returns
AsyncPage.object_listas a list. This method must be awaited beforeAsyncPagecan be treated as a sequence ofAsyncPage.object_list.
例外¶
Paginator.page() メソッドは、リクエストされたページが無効 (つまり整数ではない) か、オブジェクトが含まれていない場合に例外を発生させます。通常、 InvalidPage 例外をキャッチすれば十分ですが、より詳細に例外をキャッチしたい場合は、以下のいずれかの例外をキャッチします:
どちらの例外も InvalidPage のサブクラスなので、 except InvalidPage で処理できます。