1. ======================
    
  2. Multiple object mixins
    
  3. ======================
    
  4. 
    
  5. ``MultipleObjectMixin``
    
  6. =======================
    
  7. 
    
  8. .. class:: django.views.generic.list.MultipleObjectMixin
    
  9. 
    
  10.     A mixin that can be used to display a list of objects.
    
  11. 
    
  12.     If ``paginate_by`` is specified, Django will paginate the results returned
    
  13.     by this. You can specify the page number in the URL in one of two ways:
    
  14. 
    
  15.     * Use the ``page`` parameter in the URLconf. For example, this is what
    
  16.       your URLconf might look like::
    
  17. 
    
  18.         path('objects/page<int:page>/', PaginatedView.as_view()),
    
  19. 
    
  20.     * Pass the page number via the ``page`` query-string parameter. For
    
  21.       example, a URL would look like this::
    
  22. 
    
  23.         /objects/?page=3
    
  24. 
    
  25.     These values and lists are 1-based, not 0-based, so the first page would be
    
  26.     represented as page ``1``.
    
  27. 
    
  28.     For more on pagination, read the :doc:`pagination documentation
    
  29.     </topics/pagination>`.
    
  30. 
    
  31.     As a special case, you are also permitted to use ``last`` as a value for
    
  32.     ``page``::
    
  33. 
    
  34.         /objects/?page=last
    
  35. 
    
  36.     This allows you to access the final page of results without first having to
    
  37.     determine how many pages there are.
    
  38. 
    
  39.     Note that ``page`` *must* be either a valid page number or the value
    
  40.     ``last``; any other value for ``page`` will result in a 404 error.
    
  41. 
    
  42.     **Extends**
    
  43. 
    
  44.     * :class:`django.views.generic.base.ContextMixin`
    
  45. 
    
  46.     **Methods and Attributes**
    
  47. 
    
  48.     .. attribute:: allow_empty
    
  49. 
    
  50.         A boolean specifying whether to display the page if no objects are
    
  51.         available. If this is ``False`` and no objects are available, the view
    
  52.         will raise a 404 instead of displaying an empty page. By default, this
    
  53.         is ``True``.
    
  54. 
    
  55.     .. attribute:: model
    
  56. 
    
  57.         The model that this view will display data for. Specifying ``model
    
  58.         = Foo`` is effectively the same as specifying ``queryset =
    
  59.         Foo.objects.all()``, where ``objects`` stands for ``Foo``’s
    
  60.         :ref:`default manager <default-managers>`.
    
  61. 
    
  62.     .. attribute:: queryset
    
  63. 
    
  64.         A ``QuerySet`` that represents the objects. If provided, the value of
    
  65.         ``queryset`` supersedes the value provided for :attr:`model`.
    
  66. 
    
  67.         .. warning::
    
  68. 
    
  69.             ``queryset`` is a class attribute with a *mutable* value so care
    
  70.             must be taken when using it directly. Before using it, either call
    
  71.             its :meth:`~django.db.models.query.QuerySet.all` method or
    
  72.             retrieve it with :meth:`get_queryset` which takes care of the
    
  73.             cloning behind the scenes.
    
  74. 
    
  75.     .. attribute:: ordering
    
  76. 
    
  77.         A string or list of strings specifying the ordering to apply to the ``queryset``.
    
  78.         Valid values are the same as those for :meth:`~django.db.models.query.QuerySet.order_by`.
    
  79. 
    
  80.     .. attribute:: paginate_by
    
  81. 
    
  82.         An integer specifying how many objects should be displayed per page. If
    
  83.         this is given, the view will paginate objects with
    
  84.         ``paginate_by`` objects per page. The view will
    
  85.         expect either a ``page`` query string parameter (via ``request.GET``)
    
  86.         or a ``page`` variable specified in the URLconf.
    
  87. 
    
  88.     .. attribute:: paginate_orphans
    
  89. 
    
  90.         An integer specifying the number of "overflow" objects the last page
    
  91.         can contain. This extends the :attr:`paginate_by` limit on the last
    
  92.         page by up to ``paginate_orphans``, in order to keep the last page from
    
  93.         having a very small number of objects.
    
  94. 
    
  95.     .. attribute:: page_kwarg
    
  96. 
    
  97.         A string specifying the name to use for the page parameter.
    
  98.         The view will expect this parameter to be available either as a query
    
  99.         string parameter (via ``request.GET``) or as a kwarg variable specified
    
  100.         in the URLconf. Defaults to ``page``.
    
  101. 
    
  102.     .. attribute:: paginator_class
    
  103. 
    
  104.        The paginator class to be used for pagination. By default,
    
  105.        :class:`django.core.paginator.Paginator` is used. If the custom paginator
    
  106.        class doesn't have the same constructor interface as
    
  107.        :class:`django.core.paginator.Paginator`, you will also need to
    
  108.        provide an implementation for :meth:`get_paginator`.
    
  109. 
    
  110.     .. attribute:: context_object_name
    
  111. 
    
  112.         Designates the name of the variable to use in the context.
    
  113. 
    
  114.     .. method:: get_queryset()
    
  115. 
    
  116.         Get the list of items for this view. This must be an iterable and may
    
  117.         be a queryset (in which queryset-specific behavior will be enabled).
    
  118. 
    
  119.     .. method:: get_ordering()
    
  120. 
    
  121.         Returns a string (or iterable of strings) that defines the ordering that
    
  122.         will be applied to the ``queryset``.
    
  123. 
    
  124.         Returns :attr:`ordering` by default.
    
  125. 
    
  126.     .. method:: paginate_queryset(queryset, page_size)
    
  127. 
    
  128.         Returns a 4-tuple containing (``paginator``, ``page``, ``object_list``,
    
  129.         ``is_paginated``).
    
  130. 
    
  131.         Constructed by paginating ``queryset`` into pages of size ``page_size``.
    
  132.         If the request contains a ``page`` argument, either as a captured URL
    
  133.         argument or as a GET argument, ``object_list`` will correspond to the
    
  134.         objects from that page.
    
  135. 
    
  136.     .. method:: get_paginate_by(queryset)
    
  137. 
    
  138.         Returns the number of items to paginate by, or ``None`` for no
    
  139.         pagination. By default this returns the value of :attr:`paginate_by`.
    
  140. 
    
  141.     .. method:: get_paginator(queryset, per_page, orphans=0, allow_empty_first_page=True)
    
  142. 
    
  143.         Returns an instance of the paginator to use for this view. By default,
    
  144.         instantiates an instance of :attr:`paginator_class`.
    
  145. 
    
  146.     .. method:: get_paginate_orphans()
    
  147. 
    
  148.         An integer specifying the number of "overflow" objects the last page
    
  149.         can contain. By default this returns the value of
    
  150.         :attr:`paginate_orphans`.
    
  151. 
    
  152.     .. method:: get_allow_empty()
    
  153. 
    
  154.         Return a boolean specifying whether to display the page if no objects
    
  155.         are available. If this method returns ``False`` and no objects are
    
  156.         available, the view will raise a 404 instead of displaying an empty
    
  157.         page. By default, this is ``True``.
    
  158. 
    
  159.     .. method:: get_context_object_name(object_list)
    
  160. 
    
  161.         Return the context variable name that will be used to contain
    
  162.         the list of data that this view is manipulating. If
    
  163.         ``object_list`` is a queryset of Django objects and
    
  164.         :attr:`context_object_name` is not set,
    
  165.         the context name will be the ``model_name`` of the model that
    
  166.         the queryset is composed from, with postfix ``'_list'``
    
  167.         appended. For example, the model ``Article`` would have a
    
  168.         context object named ``article_list``.
    
  169. 
    
  170.     .. method:: get_context_data(**kwargs)
    
  171. 
    
  172.         Returns context data for displaying the list of objects.
    
  173. 
    
  174.     **Context**
    
  175. 
    
  176.     * ``object_list``: The list of objects that this view is displaying. If
    
  177.       ``context_object_name`` is specified, that variable will also be set
    
  178.       in the context, with the same value as ``object_list``.
    
  179. 
    
  180.     * ``is_paginated``: A boolean representing whether the results are
    
  181.       paginated. Specifically, this is set to ``False`` if no page size has
    
  182.       been specified, or if the available objects do not span multiple
    
  183.       pages.
    
  184. 
    
  185.     * ``paginator``: An instance of
    
  186.       :class:`django.core.paginator.Paginator`. If the page is not
    
  187.       paginated, this context variable will be ``None``.
    
  188. 
    
  189.     * ``page_obj``: An instance of
    
  190.       :class:`django.core.paginator.Page`. If the page is not paginated,
    
  191.       this context variable will be ``None``.
    
  192. 
    
  193. 
    
  194. ``MultipleObjectTemplateResponseMixin``
    
  195. =======================================
    
  196. 
    
  197. .. class:: django.views.generic.list.MultipleObjectTemplateResponseMixin
    
  198. 
    
  199.     A mixin class that performs template-based response rendering for views
    
  200.     that operate upon a list of object instances. Requires that the view it is
    
  201.     mixed with provides ``self.object_list``, the list of object instances that
    
  202.     the view is operating on. ``self.object_list`` may be, but is not required
    
  203.     to be, a :class:`~django.db.models.query.QuerySet`.
    
  204. 
    
  205.     **Extends**
    
  206. 
    
  207.     * :class:`~django.views.generic.base.TemplateResponseMixin`
    
  208. 
    
  209.     **Methods and Attributes**
    
  210. 
    
  211.     .. attribute:: template_name_suffix
    
  212. 
    
  213.         The suffix to append to the auto-generated candidate template name.
    
  214.         Default suffix is ``_list``.
    
  215. 
    
  216.     .. method:: get_template_names()
    
  217. 
    
  218.         Returns a list of candidate template names. Returns the following list:
    
  219. 
    
  220.         * the value of ``template_name`` on the view (if provided)
    
  221.         * ``<app_label>/<model_name><template_name_suffix>.html``