ページ分割 (Paginator)Link to this heading
Django はページ分割されたデータを管理するのに役立ついくつかのクラスを提供しています。これらのクラスは django/core/paginator.py にあります。
例については ページ分割 (Pagination) のトピックガイド を参照してください。
Paginator classLink to this heading
- class Paginator(object_list, per_page, orphans=0, allow_empty_first_page=True, error_messages=None)Link to this definition
Paginator(ページネーター)は
len()の使用時、または直接イテレートしたときはPageのシーケンスのように動作します。
- Paginator.object_listLink to this definition
必須です。リスト、タプル、
QuerySet、またはcount()または__len__()メソッドを持つその他のスライス可能なオブジェクト。一貫したページ分割のためには、QuerySetは順序付けされるべきです。たとえば、order_by()句を使用するか、モデル上のデフォルトのorderingで行います。
- Paginator.per_pageLink to this definition
必須です。ページ上に含めるアイテムの最大数で、孤立アイテムは含みません(以下の
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引数を指定すると、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
メソッド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
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を発生させます。
属性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
1つのページは、
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オブジェクトでページ分割している場合、2ページ目の
start_index()は3を返すでしょう。
- Page.end_index()Link to this definition
ページ上の最後のオブジェクトに対する、1から始まるインデックスを返します。これは、ページネータのリストに含まれる全オブジェクトに対するインデックスです。たとえば、5個のオブジェクトのリストを各ページ2オブジェクトでページ分割している場合、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
pagenator に無効なページ数が渡された時に発生する例外のベースクラスです。
Paginator.page() メソッドは、リクエストされたページが無効 (つまり整数ではない) か、オブジェクトが含まれていない場合に例外を発生させます。通常、 InvalidPage 例外をキャッチすれば十分ですが、より詳細に例外をキャッチしたい場合は、以下のいずれかの例外をキャッチします:
- exception PageNotAnIntegerLink to this definition
page()に整数でない値が与えられた時に発生します。
- exception EmptyPageLink to this definition
page()に有効な値が与えられているが、そのページにオブジェクトが存在しない場合に発生します。
どちらの例外も InvalidPage のサブクラスなので、 except InvalidPage で処理できます。