mixins-date-based.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. =================
  2. Date-based mixins
  3. =================
  4. .. currentmodule:: django.views.generic.dates
  5. .. note::
  6. All the date formatting attributes in these mixins use
  7. :func:`~time.strftime` format characters. Do not try to use the format
  8. characters from the :ttag:`now` template tag as they are not compatible.
  9. ``YearMixin``
  10. =============
  11. .. class:: YearMixin
  12. A mixin that can be used to retrieve and provide parsing information for a
  13. year component of a date.
  14. **Methods and Attributes**
  15. .. attribute:: year_format
  16. The :func:`~time.strftime` format to use when parsing the year.
  17. By default, this is ``'%Y'``.
  18. .. attribute:: year
  19. **Optional** The value for the year, as a string. By default, set to
  20. ``None``, which means the year will be determined using other means.
  21. .. method:: get_year_format()
  22. Returns the :func:`~time.strftime` format to use when parsing the
  23. year. Returns :attr:`~YearMixin.year_format` by default.
  24. .. method:: get_year()
  25. Returns the year for which this view will display data, as a string.
  26. Tries the following sources, in order:
  27. * The value of the :attr:`YearMixin.year` attribute.
  28. * The value of the ``year`` argument captured in the URL pattern.
  29. * The value of the ``year`` ``GET`` query argument.
  30. Raises a 404 if no valid year specification can be found.
  31. .. method:: get_next_year(date)
  32. Returns a date object containing the first day of the year after the
  33. date provided. This function can also return ``None`` or raise an
  34. :class:`~django.http.Http404` exception, depending on the values of
  35. :attr:`~BaseDateListView.allow_empty` and
  36. :attr:`~DateMixin.allow_future`.
  37. .. method:: get_previous_year(date)
  38. Returns a date object containing the first day of the year before the
  39. date provided. This function can also return ``None`` or raise an
  40. :class:`~django.http.Http404` exception, depending on the values of
  41. :attr:`~BaseDateListView.allow_empty` and
  42. :attr:`~DateMixin.allow_future`.
  43. ``MonthMixin``
  44. ==============
  45. .. class:: MonthMixin
  46. A mixin that can be used to retrieve and provide parsing information for a
  47. month component of a date.
  48. **Methods and Attributes**
  49. .. attribute:: month_format
  50. The :func:`~time.strftime` format to use when parsing the month. By
  51. default, this is ``'%b'``.
  52. .. attribute:: month
  53. **Optional** The value for the month, as a string. By default, set to
  54. ``None``, which means the month will be determined using other means.
  55. .. method:: get_month_format()
  56. Returns the :func:`~time.strftime` format to use when parsing the
  57. month. Returns :attr:`~MonthMixin.month_format` by default.
  58. .. method:: get_month()
  59. Returns the month for which this view will display data, as a string.
  60. Tries the following sources, in order:
  61. * The value of the :attr:`MonthMixin.month` attribute.
  62. * The value of the ``month`` argument captured in the URL pattern.
  63. * The value of the ``month`` ``GET`` query argument.
  64. Raises a 404 if no valid month specification can be found.
  65. .. method:: get_next_month(date)
  66. Returns a date object containing the first day of the month after the
  67. date provided. This function can also return ``None`` or raise an
  68. :class:`~django.http.Http404` exception, depending on the values of
  69. :attr:`~BaseDateListView.allow_empty` and
  70. :attr:`~DateMixin.allow_future`.
  71. .. method:: get_previous_month(date)
  72. Returns a date object containing the first day of the month before the
  73. date provided. This function can also return ``None`` or raise an
  74. :class:`~django.http.Http404` exception, depending on the values of
  75. :attr:`~BaseDateListView.allow_empty` and
  76. :attr:`~DateMixin.allow_future`.
  77. ``DayMixin``
  78. ============
  79. .. class:: DayMixin
  80. A mixin that can be used to retrieve and provide parsing information for a
  81. day component of a date.
  82. **Methods and Attributes**
  83. .. attribute:: day_format
  84. The :func:`~time.strftime` format to use when parsing the day. By
  85. default, this is ``'%d'``.
  86. .. attribute:: day
  87. **Optional** The value for the day, as a string. By default, set to
  88. ``None``, which means the day will be determined using other means.
  89. .. method:: get_day_format()
  90. Returns the :func:`~time.strftime` format to use when parsing the day.
  91. Returns :attr:`~DayMixin.day_format` by default.
  92. .. method:: get_day()
  93. Returns the day for which this view will display data, as a string.
  94. Tries the following sources, in order:
  95. * The value of the :attr:`DayMixin.day` attribute.
  96. * The value of the ``day`` argument captured in the URL pattern.
  97. * The value of the ``day`` ``GET`` query argument.
  98. Raises a 404 if no valid day specification can be found.
  99. .. method:: get_next_day(date)
  100. Returns a date object containing the next valid day after the date
  101. provided. This function can also return ``None`` or raise an
  102. :class:`~django.http.Http404` exception, depending on the values of
  103. :attr:`~BaseDateListView.allow_empty` and
  104. :attr:`~DateMixin.allow_future`.
  105. .. method:: get_previous_day(date)
  106. Returns a date object containing the previous valid day. This function
  107. can also return ``None`` or raise an :class:`~django.http.Http404`
  108. exception, depending on the values of
  109. :attr:`~BaseDateListView.allow_empty` and
  110. :attr:`~DateMixin.allow_future`.
  111. ``WeekMixin``
  112. =============
  113. .. class:: WeekMixin
  114. A mixin that can be used to retrieve and provide parsing information for a
  115. week component of a date.
  116. **Methods and Attributes**
  117. .. attribute:: week_format
  118. The :func:`~time.strftime` format to use when parsing the week. By
  119. default, this is ``'%U'``, which means the week starts on Sunday. Set
  120. it to ``'%W'`` or ``'%V'`` (ISO 8601 week) if your week starts on
  121. Monday.
  122. .. versionadded:: 3.2
  123. Support for the ``'%V'`` week format was added.
  124. .. attribute:: week
  125. **Optional** The value for the week, as a string. By default, set to
  126. ``None``, which means the week will be determined using other means.
  127. .. method:: get_week_format()
  128. Returns the :func:`~time.strftime` format to use when parsing the
  129. week. Returns :attr:`~WeekMixin.week_format` by default.
  130. .. method:: get_week()
  131. Returns the week for which this view will display data, as a string.
  132. Tries the following sources, in order:
  133. * The value of the :attr:`WeekMixin.week` attribute.
  134. * The value of the ``week`` argument captured in the URL pattern
  135. * The value of the ``week`` ``GET`` query argument.
  136. Raises a 404 if no valid week specification can be found.
  137. .. method:: get_next_week(date)
  138. Returns a date object containing the first day of the week after the
  139. date provided. This function can also return ``None`` or raise an
  140. :class:`~django.http.Http404` exception, depending on the values of
  141. :attr:`~BaseDateListView.allow_empty` and
  142. :attr:`~DateMixin.allow_future`.
  143. .. method:: get_prev_week(date)
  144. Returns a date object containing the first day of the week before the
  145. date provided. This function can also return ``None`` or raise an
  146. :class:`~django.http.Http404` exception, depending on the values of
  147. :attr:`~BaseDateListView.allow_empty` and
  148. :attr:`~DateMixin.allow_future`.
  149. ``DateMixin``
  150. =============
  151. .. class:: DateMixin
  152. A mixin class providing common behavior for all date-based views.
  153. **Methods and Attributes**
  154. .. attribute:: date_field
  155. The name of the ``DateField`` or ``DateTimeField`` in the
  156. ``QuerySet``’s model that the date-based archive should use to
  157. determine the list of objects to display on the page.
  158. When :doc:`time zone support </topics/i18n/timezones>` is enabled and
  159. ``date_field`` is a ``DateTimeField``, dates are assumed to be in the
  160. current time zone. Otherwise, the queryset could include objects from
  161. the previous or the next day in the end user's time zone.
  162. .. warning::
  163. In this situation, if you have implemented per-user time zone
  164. selection, the same URL may show a different set of objects,
  165. depending on the end user's time zone. To avoid this, you should
  166. use a ``DateField`` as the ``date_field`` attribute.
  167. .. attribute:: allow_future
  168. A boolean specifying whether to include "future" objects on this page,
  169. where "future" means objects in which the field specified in
  170. ``date_field`` is greater than the current date/time. By default, this
  171. is ``False``.
  172. .. method:: get_date_field()
  173. Returns the name of the field that contains the date data that this
  174. view will operate on. Returns :attr:`~DateMixin.date_field` by default.
  175. .. method:: get_allow_future()
  176. Determine whether to include "future" objects on this page, where
  177. "future" means objects in which the field specified in ``date_field``
  178. is greater than the current date/time. Returns
  179. :attr:`~DateMixin.allow_future` by default.
  180. ``BaseDateListView``
  181. ====================
  182. .. class:: BaseDateListView
  183. A base class that provides common behavior for all date-based views. There
  184. won't normally be a reason to instantiate
  185. :class:`~django.views.generic.dates.BaseDateListView`; instantiate one of
  186. the subclasses instead.
  187. While this view (and its subclasses) are executing, ``self.object_list``
  188. will contain the list of objects that the view is operating upon, and
  189. ``self.date_list`` will contain the list of dates for which data is
  190. available.
  191. **Mixins**
  192. * :class:`~django.views.generic.dates.DateMixin`
  193. * :class:`~django.views.generic.list.MultipleObjectMixin`
  194. **Methods and Attributes**
  195. .. attribute:: allow_empty
  196. A boolean specifying whether to display the page if no objects are
  197. available. If this is ``True`` and no objects are available, the view
  198. will display an empty page instead of raising a 404.
  199. This is identical to
  200. :attr:`django.views.generic.list.MultipleObjectMixin.allow_empty`,
  201. except for the default value, which is ``False``.
  202. .. attribute:: date_list_period
  203. **Optional** A string defining the aggregation period for
  204. ``date_list``. It must be one of ``'year'`` (default), ``'month'``, or
  205. ``'day'``.
  206. .. method:: get_dated_items()
  207. Returns a 3-tuple containing (``date_list``, ``object_list``,
  208. ``extra_context``).
  209. ``date_list`` is the list of dates for which data is available.
  210. ``object_list`` is the list of objects. ``extra_context`` is a
  211. dictionary of context data that will be added to any context data
  212. provided by the
  213. :class:`~django.views.generic.list.MultipleObjectMixin`.
  214. .. method:: get_dated_queryset(**lookup)
  215. Returns a queryset, filtered using the query arguments defined by
  216. ``lookup``. Enforces any restrictions on the queryset, such as
  217. ``allow_empty`` and ``allow_future``.
  218. .. method:: get_date_list_period()
  219. Returns the aggregation period for ``date_list``. Returns
  220. :attr:`~BaseDateListView.date_list_period` by default.
  221. .. method:: get_date_list(queryset, date_type=None, ordering='ASC')
  222. Returns the list of dates of type ``date_type`` for which ``queryset``
  223. contains entries. For example, ``get_date_list(qs, 'year')`` will
  224. return the list of years for which ``qs`` has entries. If
  225. ``date_type`` isn't provided, the result of
  226. :meth:`~BaseDateListView.get_date_list_period` is used. ``date_type``
  227. and ``ordering`` are passed to
  228. :meth:`QuerySet.dates()<django.db.models.query.QuerySet.dates>`.