Multiple object mixinsLink to this heading

MultipleObjectMixinLink to this heading

class django.views.generic.list.MultipleObjectMixinLink to this definition

オブジェクトのリストを表示するために使用する Mixin です。

paginate_by が指定された場合、Django は結果をページネートします。URL 内のページ数は以下のいずれかの方法で指定します:

  • URLconf 内で page パラメータを使用します。例えば、URLconf は以下のようになります:

    Code
    path("objects/page<int:page>/", PaginatedView.as_view()),
    
  • Pass the page number via the page query-string parameter. For example, a URL would look like this:

    Text
    /objects/?page=3
    

これらの値は (0 ベースではなく) 1 ベースなので、最初のページはページ 1 で表示されます。

ページネーションの詳細については、ページネーションのドキュメント を参照してください。

As a special case, you are also permitted to use last as a value for page:

Text
/objects/?page=last

これによって、自分で何ページ存在するかを調べることなく、最後のページにアクセスすることができます。

注意すべきなのは、page は有効なページ数ないし値 lastどちらかでなければならない ことです。page に対する他の値はすべて 404 エラーとなります。

Extends

メソッドと属性

allow_emptyLink to this definition

有効なオブジェクトが 1 つもない場合にページを表示するかどうかを指定する真偽値です。False に指定してオブジェクトが存在しない場合、空のページの代わりに 404 を投げます。デフォルトは True です。

modelLink to this definition

このビューがデータを表示する対象のモデルです。model = Foo と指定することは、queryset = Foo.objects.all() の効率的な書き方で、 objectsFooデフォルトマネージャ を表します。

querysetLink to this definition

オブジェクトを表す QuerySet です。指定すると、queryset の値は model の結果を上書きします。

orderingLink to this definition

queryset に適用される並び順を指定するための文字列ないし文字列のリストです。order_by() に対するものと同じ値が有効です。

paginate_byLink to this definition

何個のオブジェクトが各ページに表示されるべきかを指定する数値です。この値が指定されると、各ページにおいて``paginate_by`` 数のオブジェクトをページネートします。ビューには (request.GET を通じた) page クエリ文字列パラメータ、もしくは URLconf で page 変数が指定されることを必要とします。

paginate_orphansLink to this definition

最後のページが含むことができる "はみ出た" オブジェクトの数を指定する数値です。最後のページで、paginate_by の制限を最大で paginate_orphans の値まで拡張し、最後のページでほんの少しのオブジェクトしか表示されないことを防ぎます。

page_kwargLink to this definition

ページのパラメータに使用する名前を指定する文字列です。ビューは (request.GET を通じた) クエリ文字列パラメータ、もしくはURLconf 内で指定した kwarg が有効になっていることを必要とします。デフォルトは page です。

paginator_classLink to this definition

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_nameLink to this definition

Designates the name of the variable to use in the context.

get_queryset()Link to this definition

このビューのための項目のリストを取得します。これは必ずイテラブルにするべきであり、クエリセットにされることが多いです(クエリセット特有の挙動を有効にするため)。

get_ordering()Link to this definition

queryset に適用される順序を定義する、文字列(または文字列のイテラブル)を返します。

Returns ordering by default.

paginate_queryset(queryset, page_size)Link to this definition

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)Link to this definition

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)Link to this definition

Returns an instance of the paginator to use for this view. By default, instantiates an instance of paginator_class.

get_paginate_orphans()Link to this definition

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()Link to this definition

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)Link to this definition

このビューが操作しているデータのリストを格納するために使用される、コンテキストの変数名を返します。もし object_list がDjangoオブジェクトのクエリセットであり、 context_object_name が設定されていない場合、コンテキスト名はクエリセットを構成するモデルの model_name となり、接尾語として '_list' が付記されます。たとえば、 Article モデルなら article_list という名前のコンテキストオブジェクトを保持します。

get_context_data(**kwargs)Link to this definition

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.

MultipleObjectTemplateResponseMixinLink to this heading

class django.views.generic.list.MultipleObjectTemplateResponseMixinLink to this definition

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_suffixLink to this definition

The suffix to append to the auto-generated candidate template name. Default suffix is _list.

get_template_names()Link to this definition

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