mixins-multiple-object.txt 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. ======================
  2. Multiple object mixins
  3. ======================
  4. MultipleObjectMixin
  5. -------------------
  6. .. class:: django.views.generic.list.MultipleObjectMixin
  7. A mixin that can be used to display a list of objects.
  8. If ``paginate_by`` is specified, Django will paginate the results returned
  9. by this. You can specify the page number in the URL in one of two ways:
  10. * Use the ``page`` parameter in the URLconf. For example, this is what
  11. your URLconf might look like::
  12. (r'^objects/page(?P<page>[0-9]+)/$', PaginatedView.as_view())
  13. * Pass the page number via the ``page`` query-string parameter. For
  14. example, a URL would look like this::
  15. /objects/?page=3
  16. These values and lists are 1-based, not 0-based, so the first page would be
  17. represented as page ``1``.
  18. For more on pagination, read the :doc:`pagination documentation
  19. </topics/pagination>`.
  20. As a special case, you are also permitted to use ``last`` as a value for
  21. ``page``::
  22. /objects/?page=last
  23. This allows you to access the final page of results without first having to
  24. determine how many pages there are.
  25. Note that ``page`` *must* be either a valid page number or the value
  26. ``last``; any other value for ``page`` will result in a 404 error.
  27. **Extends**
  28. * :class:`django.views.generic.base.ContextMixin`
  29. **Methods and Attributes**
  30. .. attribute:: allow_empty
  31. A boolean specifying whether to display the page if no objects are
  32. available. If this is ``False`` and no objects are available, the view
  33. will raise a 404 instead of displaying an empty page. By default, this
  34. is ``True``.
  35. .. attribute:: model
  36. The model that this view will display data for. Specifying ``model
  37. = Foo`` is effectively the same as specifying ``queryset =
  38. Foo.objects.all()``.
  39. .. attribute:: queryset
  40. A ``QuerySet`` that represents the objects. If provided, the value of
  41. :attr:`MultipleObjectMixin.queryset` supersedes the value provided for
  42. :attr:`MultipleObjectMixin.model`.
  43. .. attribute:: paginate_by
  44. An integer specifying how many objects should be displayed per page. If
  45. this is given, the view will paginate objects with
  46. :attr:`MultipleObjectMixin.paginate_by` objects per page. The view will
  47. expect either a ``page`` query string parameter (via ``request.GET``)
  48. or a ``page`` variable specified in the URLconf.
  49. .. attribute:: page_kwarg
  50. A string specifying the name to use for the page parameter.
  51. The view will expect this prameter to be available either as a query
  52. string parameter (via ``request.GET``) or as a kwarg variable specified
  53. in the URLconf. Defaults to ``"page"``.
  54. .. attribute:: paginator_class
  55. The paginator class to be used for pagination. By default,
  56. :class:`django.core.paginator.Paginator` is used. If the custom paginator
  57. class doesn't have the same constructor interface as
  58. :class:`django.core.paginator.Paginator`, you will also need to
  59. provide an implementation for :meth:`MultipleObjectMixin.get_paginator`.
  60. .. attribute:: context_object_name
  61. Designates the name of the variable to use in the context.
  62. .. method:: get_queryset()
  63. Get the list of items for this view. This must be an iterable and may
  64. be a queryset (in which queryset-specific behavior will be enabled).
  65. .. method:: paginate_queryset(queryset, page_size)
  66. Returns a 4-tuple containing (``paginator``, ``page``, ``object_list``,
  67. ``is_paginated``).
  68. Constructed by paginating ``queryset`` into pages of size ``page_size``.
  69. If the request contains a ``page`` argument, either as a captured URL
  70. argument or as a GET argument, ``object_list`` will correspond to the
  71. objects from that page.
  72. .. method:: get_paginate_by(queryset)
  73. Returns the number of items to paginate by, or ``None`` for no
  74. pagination. By default this simply returns the value of
  75. :attr:`MultipleObjectMixin.paginate_by`.
  76. .. method:: get_paginator(queryset, per_page, orphans=0, allow_empty_first_page=True)
  77. Returns an instance of the paginator to use for this view. By default,
  78. instantiates an instance of :attr:`paginator_class`.
  79. .. method:: get_allow_empty()
  80. Return a boolean specifying whether to display the page if no objects
  81. are available. If this method returns ``False`` and no objects are
  82. available, the view will raise a 404 instead of displaying an empty
  83. page. By default, this is ``True``.
  84. .. method:: get_context_object_name(object_list)
  85. Return the context variable name that will be used to contain
  86. the list of data that this view is manipulating. If
  87. ``object_list`` is a queryset of Django objects and
  88. :attr:`~MultipleObjectMixin.context_object_name` is not set,
  89. the context name will be the ``object_name`` of the model that
  90. the queryset is composed from, with postfix ``'_list'``
  91. appended. For example, the model ``Article`` would have a
  92. context object named ``article_list``.
  93. .. method:: get_context_data(**kwargs)
  94. Returns context data for displaying the list of objects.
  95. **Context**
  96. * ``object_list``: The list of objects that this view is displaying. If
  97. ``context_object_name`` is specified, that variable will also be set
  98. in the context, with the same value as ``object_list``.
  99. * ``is_paginated``: A boolean representing whether the results are
  100. paginated. Specifically, this is set to ``False`` if no page size has
  101. been specified, or if the available objects do not span multiple
  102. pages.
  103. * ``paginator``: An instance of
  104. :class:`django.core.paginator.Paginator`. If the page is not
  105. paginated, this context variable will be ``None``.
  106. * ``page_obj``: An instance of
  107. :class:`django.core.paginator.Page`. If the page is not paginated,
  108. this context variable will be ``None``.
  109. MultipleObjectTemplateResponseMixin
  110. -----------------------------------
  111. .. class:: django.views.generic.list.MultipleObjectTemplateResponseMixin
  112. A mixin class that performs template-based response rendering for views
  113. that operate upon a list of object instances. Requires that the view it is
  114. mixed with provides ``self.object_list``, the list of object instances that
  115. the view is operating on. ``self.object_list`` may be, but is not required
  116. to be, a :class:`~django.db.models.query.QuerySet`.
  117. **Extends**
  118. * :class:`~django.views.generic.base.TemplateResponseMixin`
  119. **Methods and Attributes**
  120. .. attribute:: template_name_suffix
  121. The suffix to append to the auto-generated candidate template name.
  122. Default suffix is ``_list``.
  123. .. method:: get_template_names()
  124. Returns a list of candidate template names. Returns the following list:
  125. * the value of ``template_name`` on the view (if provided)
  126. * ``<app_label>/<object_name><template_name_suffix>.html``