2012-06-11 16:34:00 +08:00
|
|
|
=================
|
|
|
|
Date-based mixins
|
|
|
|
=================
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
.. currentmodule:: django.views.generic.dates
|
2012-06-11 16:34:00 +08:00
|
|
|
|
2012-08-11 14:07:15 +08:00
|
|
|
YearMixin
|
|
|
|
---------
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
.. class:: YearMixin
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
A mixin that can be used to retrieve and provide parsing information for a
|
|
|
|
year component of a date.
|
|
|
|
|
|
|
|
**Methods and Attributes**
|
|
|
|
|
|
|
|
.. attribute:: year_format
|
|
|
|
|
|
|
|
The :func:`~time.strftime` format to use when parsing the year.
|
|
|
|
By default, this is ``'%Y'``.
|
|
|
|
|
|
|
|
.. attribute:: year
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
**Optional** The value for the year, as a string. By default, set to
|
2012-06-11 16:34:00 +08:00
|
|
|
``None``, which means the year will be determined using other means.
|
|
|
|
|
|
|
|
.. method:: get_year_format()
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
Returns the :func:`~time.strftime` format to use when parsing the
|
|
|
|
year. Returns :attr:`~YearMixin.year_format` by default.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
.. method:: get_year()
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
Returns the year for which this view will display data, as a string.
|
|
|
|
Tries the following sources, in order:
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
* The value of the :attr:`YearMixin.year` attribute.
|
2012-09-09 00:20:20 +08:00
|
|
|
* The value of the `year` argument captured in the URL pattern.
|
2012-06-11 16:34:00 +08:00
|
|
|
* The value of the `year` GET query argument.
|
|
|
|
|
|
|
|
Raises a 404 if no valid year specification can be found.
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
.. method:: get_next_year(date)
|
|
|
|
|
|
|
|
Returns a date object containing the first day of the year after the
|
|
|
|
date provided. This function can also return ``None`` or raise an
|
|
|
|
:class:`~django.http.Http404` exception, depending on the values of
|
|
|
|
:attr:`~BaseDateListView.allow_empty` and
|
|
|
|
:attr:`~DateMixin.allow_future`.
|
|
|
|
|
|
|
|
.. method:: get_previous_year(date)
|
|
|
|
|
|
|
|
Returns a date object containing the first day of the year before the
|
|
|
|
date provided. This function can also return ``None`` or raise an
|
|
|
|
:class:`~django.http.Http404` exception, depending on the values of
|
|
|
|
:attr:`~BaseDateListView.allow_empty` and
|
|
|
|
:attr:`~DateMixin.allow_future`.
|
|
|
|
|
2012-08-11 14:07:15 +08:00
|
|
|
MonthMixin
|
|
|
|
----------
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
.. class:: MonthMixin
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
A mixin that can be used to retrieve and provide parsing information for a
|
|
|
|
month component of a date.
|
|
|
|
|
|
|
|
**Methods and Attributes**
|
|
|
|
|
|
|
|
.. attribute:: month_format
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
The :func:`~time.strftime` format to use when parsing the month. By
|
|
|
|
default, this is ``'%b'``.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
.. attribute:: month
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
**Optional** The value for the month, as a string. By default, set to
|
2012-06-11 16:34:00 +08:00
|
|
|
``None``, which means the month will be determined using other means.
|
|
|
|
|
|
|
|
.. method:: get_month_format()
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
Returns the :func:`~time.strftime` format to use when parsing the
|
|
|
|
month. Returns :attr:`~MonthMixin.month_format` by default.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
.. method:: get_month()
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
Returns the month for which this view will display data, as a string.
|
|
|
|
Tries the following sources, in order:
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
* The value of the :attr:`MonthMixin.month` attribute.
|
2012-09-09 00:20:20 +08:00
|
|
|
* The value of the `month` argument captured in the URL pattern.
|
2012-06-11 16:34:00 +08:00
|
|
|
* The value of the `month` GET query argument.
|
|
|
|
|
|
|
|
Raises a 404 if no valid month specification can be found.
|
|
|
|
|
|
|
|
.. method:: get_next_month(date)
|
|
|
|
|
|
|
|
Returns a date object containing the first day of the month after the
|
2012-09-09 00:20:20 +08:00
|
|
|
date provided. This function can also return ``None`` or raise an
|
|
|
|
:class:`~django.http.Http404` exception, depending on the values of
|
|
|
|
:attr:`~BaseDateListView.allow_empty` and
|
|
|
|
:attr:`~DateMixin.allow_future`.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
.. method:: get_previous_month(date)
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
Returns a date object containing the first day of the month before the
|
2012-09-09 00:20:20 +08:00
|
|
|
date provided. This function can also return ``None`` or raise an
|
|
|
|
:class:`~django.http.Http404` exception, depending on the values of
|
|
|
|
:attr:`~BaseDateListView.allow_empty` and
|
|
|
|
:attr:`~DateMixin.allow_future`.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
2012-08-11 14:07:15 +08:00
|
|
|
DayMixin
|
|
|
|
--------
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
.. class:: DayMixin
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
A mixin that can be used to retrieve and provide parsing information for a
|
|
|
|
day component of a date.
|
|
|
|
|
|
|
|
**Methods and Attributes**
|
|
|
|
|
|
|
|
.. attribute:: day_format
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
The :func:`~time.strftime` format to use when parsing the day. By
|
|
|
|
default, this is ``'%d'``.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
.. attribute:: day
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
**Optional** The value for the day, as a string. By default, set to
|
2012-06-11 16:34:00 +08:00
|
|
|
``None``, which means the day will be determined using other means.
|
|
|
|
|
|
|
|
.. method:: get_day_format()
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
Returns the :func:`~time.strftime` format to use when parsing the day.
|
|
|
|
Returns :attr:`~DayMixin.day_format` by default.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
.. method:: get_day()
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
Returns the day for which this view will display data, as a string.
|
|
|
|
Tries the following sources, in order:
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
* The value of the :attr:`DayMixin.day` attribute.
|
2012-09-09 00:20:20 +08:00
|
|
|
* The value of the `day` argument captured in the URL pattern.
|
2012-06-11 16:34:00 +08:00
|
|
|
* The value of the `day` GET query argument.
|
|
|
|
|
|
|
|
Raises a 404 if no valid day specification can be found.
|
|
|
|
|
|
|
|
.. method:: get_next_day(date)
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
Returns a date object containing the next valid day after the date
|
|
|
|
provided. This function can also return ``None`` or raise an
|
|
|
|
:class:`~django.http.Http404` exception, depending on the values of
|
|
|
|
:attr:`~BaseDateListView.allow_empty` and
|
|
|
|
:attr:`~DateMixin.allow_future`.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
.. method:: get_previous_day(date)
|
2012-06-11 16:34:00 +08:00
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
Returns a date object containing the previous valid day. This function
|
|
|
|
can also return ``None`` or raise an :class:`~django.http.Http404`
|
|
|
|
exception, depending on the values of
|
|
|
|
:attr:`~BaseDateListView.allow_empty` and
|
|
|
|
:attr:`~DateMixin.allow_future`.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
2012-08-11 14:07:15 +08:00
|
|
|
WeekMixin
|
|
|
|
---------
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
.. class:: WeekMixin
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
A mixin that can be used to retrieve and provide parsing information for a
|
|
|
|
week component of a date.
|
|
|
|
|
|
|
|
**Methods and Attributes**
|
|
|
|
|
|
|
|
.. attribute:: week_format
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
The :func:`~time.strftime` format to use when parsing the week. By
|
|
|
|
default, this is ``'%U'``, which means the week starts on Sunday. Set
|
|
|
|
it to ``'%W'`` if your week starts on Monday.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
.. attribute:: week
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
**Optional** The value for the week, as a string. By default, set to
|
2012-06-11 16:34:00 +08:00
|
|
|
``None``, which means the week will be determined using other means.
|
|
|
|
|
|
|
|
.. method:: get_week_format()
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
Returns the :func:`~time.strftime` format to use when parsing the
|
|
|
|
week. Returns :attr:`~WeekMixin.week_format` by default.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
.. method:: get_week()
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
Returns the week for which this view will display data, as a string.
|
|
|
|
Tries the following sources, in order:
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
* The value of the :attr:`WeekMixin.week` attribute.
|
|
|
|
* The value of the `week` argument captured in the URL pattern
|
|
|
|
* The value of the `week` GET query argument.
|
|
|
|
|
|
|
|
Raises a 404 if no valid week specification can be found.
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
.. method:: get_next_week(date)
|
|
|
|
|
|
|
|
Returns a date object containing the first day of the week after the
|
|
|
|
date provided. This function can also return ``None`` or raise an
|
|
|
|
:class:`~django.http.Http404` exception, depending on the values of
|
|
|
|
:attr:`~BaseDateListView.allow_empty` and
|
|
|
|
:attr:`~DateMixin.allow_future`.
|
|
|
|
|
|
|
|
.. method:: get_prev_week(date)
|
|
|
|
|
|
|
|
Returns a date object containing the first day of the week before the
|
|
|
|
date provided. This function can also return ``None`` or raise an
|
|
|
|
:class:`~django.http.Http404` exception, depending on the values of
|
|
|
|
:attr:`~BaseDateListView.allow_empty` and
|
|
|
|
:attr:`~DateMixin.allow_future`.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
2012-08-11 14:07:15 +08:00
|
|
|
DateMixin
|
|
|
|
---------
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
.. class:: DateMixin
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
A mixin class providing common behavior for all date-based views.
|
|
|
|
|
|
|
|
**Methods and Attributes**
|
|
|
|
|
|
|
|
.. attribute:: date_field
|
|
|
|
|
|
|
|
The name of the ``DateField`` or ``DateTimeField`` in the
|
|
|
|
``QuerySet``'s model that the date-based archive should use to
|
2012-09-09 00:20:20 +08:00
|
|
|
determine the list of objects to display on the page.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
When :doc:`time zone support </topics/i18n/timezones>` is enabled and
|
|
|
|
``date_field`` is a ``DateTimeField``, dates are assumed to be in the
|
|
|
|
current time zone. Otherwise, the queryset could include objects from
|
|
|
|
the previous or the next day in the end user's time zone.
|
|
|
|
|
|
|
|
.. warning::
|
|
|
|
|
|
|
|
In this situation, if you have implemented per-user time zone
|
|
|
|
selection, the same URL may show a different set of objects,
|
|
|
|
depending on the end user's time zone. To avoid this, you should
|
|
|
|
use a ``DateField`` as the ``date_field`` attribute.
|
|
|
|
|
|
|
|
.. attribute:: allow_future
|
|
|
|
|
|
|
|
A boolean specifying whether to include "future" objects on this page,
|
|
|
|
where "future" means objects in which the field specified in
|
|
|
|
``date_field`` is greater than the current date/time. By default, this
|
|
|
|
is ``False``.
|
|
|
|
|
|
|
|
.. method:: get_date_field()
|
|
|
|
|
|
|
|
Returns the name of the field that contains the date data that this
|
2012-09-09 00:20:20 +08:00
|
|
|
view will operate on. Returns :attr:`~DateMixin.date_field` by default.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
.. method:: get_allow_future()
|
|
|
|
|
|
|
|
Determine whether to include "future" objects on this page, where
|
|
|
|
"future" means objects in which the field specified in ``date_field``
|
|
|
|
is greater than the current date/time. Returns
|
2012-09-09 00:20:20 +08:00
|
|
|
:attr:`~DateMixin.allow_future` by default.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
2012-08-11 14:07:15 +08:00
|
|
|
BaseDateListView
|
|
|
|
----------------
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
.. class:: BaseDateListView
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
A base class that provides common behavior for all date-based views. There
|
|
|
|
won't normally be a reason to instantiate
|
|
|
|
:class:`~django.views.generic.dates.BaseDateListView`; instantiate one of
|
|
|
|
the subclasses instead.
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
While this view (and its subclasses) are executing, ``self.object_list``
|
2012-06-11 16:34:00 +08:00
|
|
|
will contain the list of objects that the view is operating upon, and
|
|
|
|
``self.date_list`` will contain the list of dates for which data is
|
|
|
|
available.
|
|
|
|
|
|
|
|
**Mixins**
|
|
|
|
|
|
|
|
* :class:`~django.views.generic.dates.DateMixin`
|
|
|
|
* :class:`~django.views.generic.list.MultipleObjectMixin`
|
|
|
|
|
|
|
|
**Methods and Attributes**
|
|
|
|
|
|
|
|
.. attribute:: allow_empty
|
|
|
|
|
|
|
|
A boolean specifying whether to display the page if no objects are
|
|
|
|
available. If this is ``True`` and no objects are available, the view
|
2012-09-09 00:20:20 +08:00
|
|
|
will display an empty page instead of raising a 404.
|
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
This is identical to
|
|
|
|
:attr:`django.views.generic.list.MultipleObjectMixin.allow_empty`,
|
|
|
|
except for the default value, which is ``False``.
|
2012-09-09 00:20:20 +08:00
|
|
|
|
|
|
|
.. attribute:: date_list_period
|
2012-06-11 16:34:00 +08:00
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
**Optional** A string defining the aggregation period for
|
|
|
|
``date_list``. It must be one of ``'year'`` (default), ``'month'``, or
|
|
|
|
``'day'``.
|
|
|
|
|
|
|
|
.. method:: get_dated_items()
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
Returns a 3-tuple containing (``date_list``, ``object_list``,
|
|
|
|
``extra_context``).
|
|
|
|
|
|
|
|
``date_list`` is the list of dates for which data is available.
|
|
|
|
``object_list`` is the list of objects. ``extra_context`` is a
|
|
|
|
dictionary of context data that will be added to any context data
|
|
|
|
provided by the
|
|
|
|
:class:`~django.views.generic.list.MultipleObjectMixin`.
|
|
|
|
|
|
|
|
.. method:: get_dated_queryset(**lookup)
|
|
|
|
|
|
|
|
Returns a queryset, filtered using the query arguments defined by
|
|
|
|
``lookup``. Enforces any restrictions on the queryset, such as
|
|
|
|
``allow_empty`` and ``allow_future``.
|
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
.. method:: get_date_list_period()
|
|
|
|
|
|
|
|
Returns the aggregation period for ``date_list``. Returns
|
|
|
|
:attr:`~BaseDateListView.date_list_period` by default.
|
|
|
|
|
2012-09-22 17:45:51 +08:00
|
|
|
.. method:: get_date_list(queryset, date_type=None, ordering='ASC')
|
2012-06-11 16:34:00 +08:00
|
|
|
|
2012-09-09 00:20:20 +08:00
|
|
|
Returns the list of dates of type ``date_type`` for which ``queryset``
|
|
|
|
contains entries. For example, ``get_date_list(qs, 'year')`` will
|
|
|
|
return the list of years for which ``qs`` has entries. If
|
|
|
|
``date_type`` isn't provided, the result of
|
2012-09-22 17:45:51 +08:00
|
|
|
:meth:`~BaseDateListView.get_date_list_period` is used. ``date_type``
|
|
|
|
and ``ordering`` are simply passed to
|
|
|
|
:meth:`QuerySet.dates()<django.db.models.query.QuerySet.dates>`.
|
|
|
|
|
|
|
|
.. versionchanged:: 1.5
|
|
|
|
The ``ordering`` parameter was added, and the default order was
|
|
|
|
changed to ascending.
|