Multiple object mixins¶
MultipleObjectMixin
¶
-
class
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
Methods and Attributes
-
allow_empty
¶ 有効なオブジェクトが 1 つもない場合にページを表示するかどうかを指定する真偽値です。
False
に指定してオブジェクトが存在しない場合、空のページの代わりに 404 を投げます。デフォルトはTrue
です。
-
model
¶ このビューがデータを表示する対象のモデルです。
model = Foo
と指定することは、queryset = Foo.objects.all()
の効率的な書き方で、objects
はFoo
の デフォルトマネージャ を表します。
-
queryset
¶ オブジェクトを表す
QuerySet
です。指定すると、queryset
の値はmodel
の結果を上書きします。警告
queryset
is a class attribute with a mutable value so care must be taken when using it directly. Before using it, either call itsall()
method or retrieve it withget_queryset()
which takes care of the cloning behind the scenes.
-
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 asdjango.core.paginator.Paginator
, you will also need to provide an implementation forget_paginator()
.
-
context_object_name
¶ Designates the name of the variable to use in the context.
-
get_queryset
()¶ Get the list of items for this view. This must be an iterable and may be a queryset (in which queryset-specific behavior will be enabled).
-
get_ordering
()¶ Returns a string (or iterable of strings) that defines the ordering that will be applied to the
queryset
.Returns
ordering
by default.
-
paginate_queryset
(queryset, page_size)¶ Returns a 4-tuple containing (
paginator
,page
,object_list
,is_paginated
).Constructed by paginating
queryset
into pages of sizepage_size
. If the request contains apage
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 simply returns the value ofpaginate_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 simply 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 isTrue
.
-
get_context_object_name
(object_list)¶ Return the context variable name that will be used to contain the list of data that this view is manipulating. If
object_list
is a queryset of Django objects andcontext_object_name
is not set, the context name will be themodel_name
of the model that the queryset is composed from, with postfix'_list'
appended. For example, the modelArticle
would have a context object namedarticle_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. Ifcontext_object_name
is specified, that variable will also be set in the context, with the same value asobject_list
.is_paginated
: A boolean representing whether the results are paginated. Specifically, this is set toFalse
if no page size has been specified, or if the available objects do not span multiple pages.paginator
: An instance ofdjango.core.paginator.Paginator
. If the page is not paginated, this context variable will beNone
.page_obj
: An instance ofdjango.core.paginator.Page
. If the page is not paginated, this context variable will beNone
.
MultipleObjectTemplateResponseMixin
¶
-
class
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, aQuerySet
.Extends
Methods and Attributes
-
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:
- the value of
template_name
on the view (if provided) <app_label>/<model_name><template_name_suffix>.html
- the value of
-