MultipleObjectMixin¶django.views.generic.list.MultipleObjectMixin¶オブジェクトのリストを表示するために使用する Mixin です。
paginate_by が指定された場合、Django は結果をページネートします。URL 内のページ数は以下のいずれかの方法で指定します:
URLconf 内で page パラメータを使用します。例えば、URLconf は以下のようになります:
path('objects/page<int:page>/', PaginatedView.as_view()),
page クエリ文字列パラメータを通じて、ページ数を渡します。例えば、URL は以下のようになります:
/objects/?page=3
これらの値は (0 ベースではなく) 1 ベースなので、最初のページはページ 1 で表示されます。
ページネーションの詳細については、ページネーションのドキュメント を参照してください。
特別なケースとして、page に対する値として last を使用することもできます:
/objects/?page=last
これによって、自分で何ページ存在するかを調べることなく、最後のページにアクセスすることができます。
注意すべきなのは、page は有効なページ数ないし値 last の どちらかでなければならない ことです。page に対する他の値はすべて 404 エラーとなります。
Extends
メソッドと属性
allow_empty¶有効なオブジェクトが 1 つもない場合にページを表示するかどうかを指定する真偽値です。False に指定してオブジェクトが存在しない場合、空のページの代わりに 404 を投げます。デフォルトは True です。
model¶このビューがデータを表示する対象のモデルです。model = Foo と指定することは、queryset = Foo.objects.all() の効率的な書き方で、 objects は Foo の デフォルトマネージャ を表します。
queryset¶オブジェクトを表す QuerySet です。指定すると、queryset の値は model の結果を上書きします。
警告
queryset は 編集可能な 値を伴うクラスの属性なので、直接使用する際には注意が必要です。この属性を使用する際は、 all() メソッドを呼び出すか、内部で複製処理を行う get_queryset() でこれを取得するようにしてください。
ordering¶queryset に適用される並び順を指定するための文字列ないし文字列のリストです。order_by() に対するものと同じ値が有効です。
paginate_by¶何個のオブジェクトが各ページに表示されるべきかを指定する数値です。この値が指定されると、各ページにおいて``paginate_by`` 数のオブジェクトをページネートします。ビューには (request.GET を通じた) page クエリ文字列パラメータ、もしくは URLconf で page 変数が指定されることを必要とします。
paginate_orphans¶最後のページが含むことができる "はみ出た" オブジェクトの数を指定する数値です。最後のページで、paginate_by の制限を最大で paginate_orphans の値まで拡張し、最後のページでほんの少しのオブジェクトしか表示されないことを防ぎます。
page_kwarg¶ページのパラメータに使用する名前を指定する文字列です。ビューは (request.GET を通じた) クエリ文字列パラメータ、もしくはURLconf 内で指定した kwarg が有効になっていることを必要とします。デフォルトは page です。
paginator_class¶The paginator class to be used for pagination. By default,
django.core.paginator.Paginator is used. If the custom paginator
class doesn't have the same constructor interface as
django.core.paginator.Paginator, you will also need to
provide an implementation for get_paginator().
context_object_name¶Designates the name of the variable to use in the context.
get_queryset()¶このビューのための項目のリストを取得します。これは必ずイテラブルにするべきであり、クエリセットにされることが多いです(クエリセット特有の挙動を有効にするため)。
paginate_queryset(queryset, page_size)¶Returns a 4-tuple containing (paginator, page, object_list,
is_paginated).
Constructed by paginating queryset into pages of size page_size.
If the request contains a page argument, either as a captured URL
argument or as a GET argument, object_list will correspond to the
objects from that page.
get_paginate_by(queryset)¶Returns the number of items to paginate by, or None for no
pagination. By default this returns the value of paginate_by.
get_paginator(queryset, per_page, orphans=0, allow_empty_first_page=True)¶Returns an instance of the paginator to use for this view. By default,
instantiates an instance of paginator_class.
get_paginate_orphans()¶An integer specifying the number of "overflow" objects the last page
can contain. By default this returns the value of
paginate_orphans.
get_allow_empty()¶Return a boolean specifying whether to display the page if no objects
are available. If this method returns False and no objects are
available, the view will raise a 404 instead of displaying an empty
page. By default, this is True.
get_context_object_name(object_list)¶このビューが操作しているデータのリストを格納するために使用される、コンテキストの変数名を返します。もし object_list がDjangoオブジェクトのクエリセットであり、 context_object_name が設定されていない場合、コンテキスト名はクエリセットを構成するモデルの model_name となり、接尾語として '_list' が付記されます。たとえば、 Article モデルなら article_list という名前のコンテキストオブジェクトを保持します。
get_context_data(**kwargs)¶Returns context data for displaying the list of objects.
コンテキスト
object_list: The list of objects that this view is displaying. If
context_object_name is specified, that variable will also be set
in the context, with the same value as object_list.is_paginated: A boolean representing whether the results are
paginated. Specifically, this is set to False if no page size has
been specified, or if the available objects do not span multiple
pages.paginator: An instance of
django.core.paginator.Paginator. If the page is not
paginated, this context variable will be None.page_obj: An instance of
django.core.paginator.Page. If the page is not paginated,
this context variable will be None.MultipleObjectTemplateResponseMixin¶django.views.generic.list.MultipleObjectTemplateResponseMixin¶A mixin class that performs template-based response rendering for views
that operate upon a list of object instances. Requires that the view it is
mixed with provides self.object_list, the list of object instances that
the view is operating on. self.object_list may be, but is not required
to be, a QuerySet.
Extends
メソッドと属性
template_name_suffix¶The suffix to append to the auto-generated candidate template name.
Default suffix is _list.
get_template_names()¶Returns a list of candidate template names. Returns the following list:
template_name on the view (if provided)<app_label>/<model_name><template_name_suffix>.html8月 03, 2022