2
0

mixins-date-based.txt 12 KB

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