mixins-date-based.txt 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. =================
  2. Date-based mixins
  3. =================
  4. .. class:: django.views.generic.dates.YearMixin
  5. A mixin that can be used to retrieve and provide parsing information for a
  6. year component of a date.
  7. **Methods and Attributes**
  8. .. attribute:: year_format
  9. The :func:`~time.strftime` format to use when parsing the year.
  10. By default, this is ``'%Y'``.
  11. .. attribute:: year
  12. **Optional** The value for the year (as a string). By default, set to
  13. ``None``, which means the year will be determined using other means.
  14. .. method:: get_year_format()
  15. Returns the :func:`~time.strftime` format to use when parsing the year. Returns
  16. :attr:`YearMixin.year_format` by default.
  17. .. method:: get_year()
  18. Returns the year for which this view will display data. Tries the
  19. following sources, in order:
  20. * The value of the :attr:`YearMixin.year` attribute.
  21. * The value of the `year` argument captured in the URL pattern
  22. * The value of the `year` GET query argument.
  23. Raises a 404 if no valid year specification can be found.
  24. .. class:: django.views.generic.dates.MonthMixin
  25. A mixin that can be used to retrieve and provide parsing information for a
  26. month component of a date.
  27. **Methods and Attributes**
  28. .. attribute:: month_format
  29. The :func:`~time.strftime` format to use when parsing the month. By default, this is
  30. ``'%b'``.
  31. .. attribute:: month
  32. **Optional** The value for the month (as a string). By default, set to
  33. ``None``, which means the month will be determined using other means.
  34. .. method:: get_month_format()
  35. Returns the :func:`~time.strftime` format to use when parsing the month. Returns
  36. :attr:`MonthMixin.month_format` by default.
  37. .. method:: get_month()
  38. Returns the month for which this view will display data. Tries the
  39. following sources, in order:
  40. * The value of the :attr:`MonthMixin.month` attribute.
  41. * The value of the `month` argument captured in the URL pattern
  42. * The value of the `month` GET query argument.
  43. Raises a 404 if no valid month specification can be found.
  44. .. method:: get_next_month(date)
  45. Returns a date object containing the first day of the month after the
  46. date provided. Returns ``None`` if mixed with a view that sets
  47. ``allow_future = False``, and the next month is in the future. If
  48. ``allow_empty = False``, returns the next month that contains data.
  49. .. method:: get_prev_month(date)
  50. Returns a date object containing the first day of the month before the
  51. date provided. If ``allow_empty = False``, returns the previous month
  52. that contained data.
  53. .. class:: django.views.generic.dates.DayMixin
  54. A mixin that can be used to retrieve and provide parsing information for a
  55. day component of a date.
  56. **Methods and Attributes**
  57. .. attribute:: day_format
  58. The :func:`~time.strftime` format to use when parsing the day. By default, this is
  59. ``'%d'``.
  60. .. attribute:: day
  61. **Optional** The value for the day (as a string). By default, set to
  62. ``None``, which means the day will be determined using other means.
  63. .. method:: get_day_format()
  64. Returns the :func:`~time.strftime` format to use when parsing the day. Returns
  65. :attr:`DayMixin.day_format` by default.
  66. .. method:: get_day()
  67. Returns the day for which this view will display data. Tries the
  68. following sources, in order:
  69. * The value of the :attr:`DayMixin.day` attribute.
  70. * The value of the `day` argument captured in the URL pattern
  71. * The value of the `day` GET query argument.
  72. Raises a 404 if no valid day specification can be found.
  73. .. method:: get_next_day(date)
  74. Returns a date object containing the next day after the date provided.
  75. Returns ``None`` if mixed with a view that sets ``allow_future = False``,
  76. and the next day is in the future. If ``allow_empty = False``, returns
  77. the next day that contains data.
  78. .. method:: get_prev_day(date)
  79. Returns a date object containing the previous day. If
  80. ``allow_empty = False``, returns the previous day that contained data.
  81. .. class:: django.views.generic.dates.WeekMixin
  82. A mixin that can be used to retrieve and provide parsing information for a
  83. week component of a date.
  84. **Methods and Attributes**
  85. .. attribute:: week_format
  86. The :func:`~time.strftime` format to use when parsing the week. By default, this is
  87. ``'%U'``.
  88. .. attribute:: week
  89. **Optional** The value for the week (as a string). By default, set to
  90. ``None``, which means the week will be determined using other means.
  91. .. method:: get_week_format()
  92. Returns the :func:`~time.strftime` format to use when parsing the week. Returns
  93. :attr:`WeekMixin.week_format` by default.
  94. .. method:: get_week()
  95. Returns the week for which this view will display data. Tries the
  96. following sources, in order:
  97. * The value of the :attr:`WeekMixin.week` attribute.
  98. * The value of the `week` argument captured in the URL pattern
  99. * The value of the `week` GET query argument.
  100. Raises a 404 if no valid week specification can be found.
  101. .. class:: django.views.generic.dates.DateMixin
  102. A mixin class providing common behavior for all date-based views.
  103. **Methods and Attributes**
  104. .. attribute:: date_field
  105. The name of the ``DateField`` or ``DateTimeField`` in the
  106. ``QuerySet``'s model that the date-based archive should use to
  107. determine the objects on the page.
  108. When :doc:`time zone support </topics/i18n/timezones>` is enabled and
  109. ``date_field`` is a ``DateTimeField``, dates are assumed to be in the
  110. current time zone. Otherwise, the queryset could include objects from
  111. the previous or the next day in the end user's time zone.
  112. .. warning::
  113. In this situation, if you have implemented per-user time zone
  114. selection, the same URL may show a different set of objects,
  115. depending on the end user's time zone. To avoid this, you should
  116. use a ``DateField`` as the ``date_field`` attribute.
  117. .. attribute:: allow_future
  118. A boolean specifying whether to include "future" objects on this page,
  119. where "future" means objects in which the field specified in
  120. ``date_field`` is greater than the current date/time. By default, this
  121. is ``False``.
  122. .. method:: get_date_field()
  123. Returns the name of the field that contains the date data that this
  124. view will operate on. Returns :attr:`DateMixin.date_field` by default.
  125. .. method:: get_allow_future()
  126. Determine whether to include "future" objects on this page, where
  127. "future" means objects in which the field specified in ``date_field``
  128. is greater than the current date/time. Returns
  129. :attr:`DateMixin.allow_future` by default.
  130. .. class:: django.views.generic.dates.BaseDateListView
  131. A base class that provides common behavior for all date-based views. There
  132. won't normally be a reason to instantiate
  133. :class:`~django.views.generic.dates.BaseDateListView`; instantiate one of
  134. the subclasses instead.
  135. While this view (and it's subclasses) are executing, ``self.object_list``
  136. will contain the list of objects that the view is operating upon, and
  137. ``self.date_list`` will contain the list of dates for which data is
  138. available.
  139. **Mixins**
  140. * :class:`~django.views.generic.dates.DateMixin`
  141. * :class:`~django.views.generic.list.MultipleObjectMixin`
  142. **Methods and Attributes**
  143. .. attribute:: allow_empty
  144. A boolean specifying whether to display the page if no objects are
  145. available. If this is ``True`` and no objects are available, the view
  146. will display an empty page instead of raising a 404. By default, this
  147. is ``False``.
  148. .. method:: get_dated_items():
  149. Returns a 3-tuple containing (``date_list``, ``object_list``,
  150. ``extra_context``).
  151. ``date_list`` is the list of dates for which data is available.
  152. ``object_list`` is the list of objects. ``extra_context`` is a
  153. dictionary of context data that will be added to any context data
  154. provided by the
  155. :class:`~django.views.generic.list.MultipleObjectMixin`.
  156. .. method:: get_dated_queryset(**lookup)
  157. Returns a queryset, filtered using the query arguments defined by
  158. ``lookup``. Enforces any restrictions on the queryset, such as
  159. ``allow_empty`` and ``allow_future``.
  160. .. method:: get_date_list(queryset, date_type)
  161. Returns the list of dates of type ``date_type`` for which
  162. ``queryset`` contains entries. For example, ``get_date_list(qs,
  163. 'year')`` will return the list of years for which ``qs`` has entries.
  164. See :meth:`~django.db.models.query.QuerySet.dates()` for the
  165. ways that the ``date_type`` argument can be used.