mixins-date-based.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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. .. attribute:: week
  123. **Optional** The value for the week, as a string. By default, set to
  124. ``None``, which means the week will be determined using other means.
  125. .. method:: get_week_format()
  126. Returns the :func:`~time.strftime` format to use when parsing the
  127. week. Returns :attr:`~WeekMixin.week_format` by default.
  128. .. method:: get_week()
  129. Returns the week for which this view will display data, as a string.
  130. Tries the following sources, in order:
  131. * The value of the :attr:`WeekMixin.week` attribute.
  132. * The value of the ``week`` argument captured in the URL pattern
  133. * The value of the ``week`` ``GET`` query argument.
  134. Raises a 404 if no valid week specification can be found.
  135. .. method:: get_next_week(date)
  136. Returns a date object containing the first day of the week after the
  137. date provided. This function can also return ``None`` or raise an
  138. :class:`~django.http.Http404` exception, depending on the values of
  139. :attr:`~BaseDateListView.allow_empty` and
  140. :attr:`~DateMixin.allow_future`.
  141. .. method:: get_prev_week(date)
  142. Returns a date object containing the first day of the week before the
  143. date provided. This function can also return ``None`` or raise an
  144. :class:`~django.http.Http404` exception, depending on the values of
  145. :attr:`~BaseDateListView.allow_empty` and
  146. :attr:`~DateMixin.allow_future`.
  147. ``DateMixin``
  148. =============
  149. .. class:: DateMixin
  150. A mixin class providing common behavior for all date-based views.
  151. **Methods and Attributes**
  152. .. attribute:: date_field
  153. The name of the ``DateField`` or ``DateTimeField`` in the
  154. ``QuerySet``’s model that the date-based archive should use to
  155. determine the list of objects to display on the page.
  156. When :doc:`time zone support </topics/i18n/timezones>` is enabled and
  157. ``date_field`` is a ``DateTimeField``, dates are assumed to be in the
  158. current time zone. Otherwise, the queryset could include objects from
  159. the previous or the next day in the end user's time zone.
  160. .. warning::
  161. In this situation, if you have implemented per-user time zone
  162. selection, the same URL may show a different set of objects,
  163. depending on the end user's time zone. To avoid this, you should
  164. use a ``DateField`` as the ``date_field`` attribute.
  165. .. attribute:: allow_future
  166. A boolean specifying whether to include "future" objects on this page,
  167. where "future" means objects in which the field specified in
  168. ``date_field`` is greater than the current date/time. By default, this
  169. is ``False``.
  170. .. method:: get_date_field()
  171. Returns the name of the field that contains the date data that this
  172. view will operate on. Returns :attr:`~DateMixin.date_field` by default.
  173. .. method:: get_allow_future()
  174. Determine whether to include "future" objects on this page, where
  175. "future" means objects in which the field specified in ``date_field``
  176. is greater than the current date/time. Returns
  177. :attr:`~DateMixin.allow_future` by default.
  178. ``BaseDateListView``
  179. ====================
  180. .. class:: BaseDateListView
  181. A base class that provides common behavior for all date-based views. There
  182. won't normally be a reason to instantiate
  183. :class:`~django.views.generic.dates.BaseDateListView`; instantiate one of
  184. the subclasses instead.
  185. While this view (and its subclasses) are executing, ``self.object_list``
  186. will contain the list of objects that the view is operating upon, and
  187. ``self.date_list`` will contain the list of dates for which data is
  188. available.
  189. **Mixins**
  190. * :class:`~django.views.generic.dates.DateMixin`
  191. * :class:`~django.views.generic.list.MultipleObjectMixin`
  192. **Methods and Attributes**
  193. .. attribute:: allow_empty
  194. A boolean specifying whether to display the page if no objects are
  195. available. If this is ``True`` and no objects are available, the view
  196. will display an empty page instead of raising a 404.
  197. This is identical to
  198. :attr:`django.views.generic.list.MultipleObjectMixin.allow_empty`,
  199. except for the default value, which is ``False``.
  200. .. attribute:: date_list_period
  201. **Optional** A string defining the aggregation period for
  202. ``date_list``. It must be one of ``'year'`` (default), ``'month'``, or
  203. ``'day'``.
  204. .. method:: get_dated_items()
  205. Returns a 3-tuple containing (``date_list``, ``object_list``,
  206. ``extra_context``).
  207. ``date_list`` is the list of dates for which data is available.
  208. ``object_list`` is the list of objects. ``extra_context`` is a
  209. dictionary of context data that will be added to any context data
  210. provided by the
  211. :class:`~django.views.generic.list.MultipleObjectMixin`.
  212. .. method:: get_dated_queryset(**lookup)
  213. Returns a queryset, filtered using the query arguments defined by
  214. ``lookup``. Enforces any restrictions on the queryset, such as
  215. ``allow_empty`` and ``allow_future``.
  216. .. method:: get_date_list_period()
  217. Returns the aggregation period for ``date_list``. Returns
  218. :attr:`~BaseDateListView.date_list_period` by default.
  219. .. method:: get_date_list(queryset, date_type=None, ordering='ASC')
  220. Returns the list of dates of type ``date_type`` for which ``queryset``
  221. contains entries. For example, ``get_date_list(qs, 'year')`` will
  222. return the list of years for which ``qs`` has entries. If
  223. ``date_type`` isn't provided, the result of
  224. :meth:`~BaseDateListView.get_date_list_period` is used. ``date_type``
  225. and ``ordering`` are passed to
  226. :meth:`QuerySet.dates()<django.db.models.query.QuerySet.dates>`.