2012-06-11 10:34:00 +02:00
|
|
|
==================
|
|
|
|
Generic date views
|
|
|
|
==================
|
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
.. module:: django.views.generic.dates
|
|
|
|
|
|
|
|
Date-based generic views, provided in :mod:`django.views.generic.dates`, are
|
|
|
|
views for displaying drilldown pages for date-based data.
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2012-11-28 01:26:37 +01:00
|
|
|
.. note::
|
|
|
|
|
|
|
|
Some of the examples on this page assume that an ``Article`` model has been
|
|
|
|
defined as follows in ``myapp/models.py``::
|
|
|
|
|
|
|
|
from django.db import models
|
2015-12-30 16:51:16 +01:00
|
|
|
from django.urls import reverse
|
2012-11-28 01:26:37 +01:00
|
|
|
|
2023-02-28 20:53:28 +01:00
|
|
|
|
2012-11-28 01:26:37 +01:00
|
|
|
class Article(models.Model):
|
|
|
|
title = models.CharField(max_length=200)
|
|
|
|
pub_date = models.DateField()
|
|
|
|
|
|
|
|
def get_absolute_url(self):
|
|
|
|
return reverse("article-detail", kwargs={"pk": self.pk})
|
|
|
|
|
2016-01-24 22:26:11 +01:00
|
|
|
``ArchiveIndexView``
|
|
|
|
====================
|
2012-08-11 08:07:15 +02:00
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
.. class:: ArchiveIndexView
|
2012-06-11 10:34:00 +02:00
|
|
|
|
|
|
|
A top-level index page showing the "latest" objects, by date. Objects with
|
|
|
|
a date in the *future* are not included unless you set ``allow_future`` to
|
|
|
|
``True``.
|
|
|
|
|
|
|
|
**Ancestors (MRO)**
|
|
|
|
|
|
|
|
* :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
|
|
|
|
* :class:`django.views.generic.base.TemplateResponseMixin`
|
|
|
|
* :class:`django.views.generic.dates.BaseArchiveIndexView`
|
|
|
|
* :class:`django.views.generic.dates.BaseDateListView`
|
|
|
|
* :class:`django.views.generic.list.MultipleObjectMixin`
|
|
|
|
* :class:`django.views.generic.dates.DateMixin`
|
|
|
|
* :class:`django.views.generic.base.View`
|
2012-08-11 08:07:15 +02:00
|
|
|
|
2013-02-01 13:42:30 +01:00
|
|
|
**Context**
|
|
|
|
|
|
|
|
In addition to the context provided by
|
|
|
|
:class:`django.views.generic.list.MultipleObjectMixin` (via
|
|
|
|
:class:`django.views.generic.dates.BaseDateListView`), the template's
|
|
|
|
context will be:
|
|
|
|
|
2015-09-11 17:14:35 +02:00
|
|
|
* ``date_list``: A :meth:`QuerySet <django.db.models.query.QuerySet.dates>`
|
|
|
|
object containing all years that have objects available according to
|
|
|
|
``queryset``, represented as :class:`datetime.datetime` objects, in
|
2013-02-01 13:42:30 +01:00
|
|
|
descending order.
|
|
|
|
|
2012-06-11 10:34:00 +02:00
|
|
|
**Notes**
|
|
|
|
|
|
|
|
* Uses a default ``context_object_name`` of ``latest``.
|
|
|
|
* Uses a default ``template_name_suffix`` of ``_archive``.
|
2012-08-18 17:28:17 +02:00
|
|
|
* Defaults to providing ``date_list`` by year, but this can be altered to
|
|
|
|
month or day using the attribute ``date_list_period``. This also applies
|
|
|
|
to all subclass views.
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2014-08-22 17:22:09 +02:00
|
|
|
**Example myapp/urls.py**::
|
2012-11-28 01:26:37 +01:00
|
|
|
|
2016-10-20 19:29:04 +02:00
|
|
|
from django.urls import path
|
2012-11-28 01:26:37 +01:00
|
|
|
from django.views.generic.dates import ArchiveIndexView
|
|
|
|
|
|
|
|
from myapp.models import Article
|
|
|
|
|
2014-04-02 02:46:34 +02:00
|
|
|
urlpatterns = [
|
2016-10-20 19:29:04 +02:00
|
|
|
path(
|
|
|
|
"archive/",
|
|
|
|
ArchiveIndexView.as_view(model=Article, date_field="pub_date"),
|
|
|
|
name="article_archive",
|
|
|
|
),
|
2014-04-02 02:46:34 +02:00
|
|
|
]
|
2012-11-28 01:26:37 +01:00
|
|
|
|
|
|
|
**Example myapp/article_archive.html**:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
{% for article in latest %}
|
|
|
|
<li>{{ article.pub_date }}: {{ article.title }}</li>
|
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
This will output all articles.
|
|
|
|
|
2016-01-24 22:26:11 +01:00
|
|
|
``YearArchiveView``
|
|
|
|
===================
|
2012-08-11 08:07:15 +02:00
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
.. class:: YearArchiveView
|
2012-06-11 10:34:00 +02:00
|
|
|
|
|
|
|
A yearly archive page showing all available months in a given year. Objects
|
|
|
|
with a date in the *future* are not displayed unless you set
|
|
|
|
``allow_future`` to ``True``.
|
|
|
|
|
|
|
|
**Ancestors (MRO)**
|
|
|
|
|
|
|
|
* :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
|
|
|
|
* :class:`django.views.generic.base.TemplateResponseMixin`
|
|
|
|
* :class:`django.views.generic.dates.BaseYearArchiveView`
|
|
|
|
* :class:`django.views.generic.dates.YearMixin`
|
|
|
|
* :class:`django.views.generic.dates.BaseDateListView`
|
|
|
|
* :class:`django.views.generic.list.MultipleObjectMixin`
|
|
|
|
* :class:`django.views.generic.dates.DateMixin`
|
|
|
|
* :class:`django.views.generic.base.View`
|
|
|
|
|
|
|
|
.. attribute:: make_object_list
|
|
|
|
|
|
|
|
A boolean specifying whether to retrieve the full list of objects for
|
|
|
|
this year and pass those to the template. If ``True``, the list of
|
2012-09-08 18:20:20 +02:00
|
|
|
objects will be made available to the context. If ``False``, the
|
|
|
|
``None`` queryset will be used as the object list. By default, this is
|
2012-06-11 10:34:00 +02:00
|
|
|
``False``.
|
|
|
|
|
|
|
|
.. method:: get_make_object_list()
|
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
Determine if an object list will be returned as part of the context.
|
|
|
|
Returns :attr:`~YearArchiveView.make_object_list` by default.
|
|
|
|
|
2012-06-11 10:34:00 +02:00
|
|
|
**Context**
|
|
|
|
|
|
|
|
In addition to the context provided by
|
|
|
|
:class:`django.views.generic.list.MultipleObjectMixin` (via
|
|
|
|
:class:`django.views.generic.dates.BaseDateListView`), the template's
|
|
|
|
context will be:
|
|
|
|
|
2015-09-11 17:14:35 +02:00
|
|
|
* ``date_list``: A :meth:`QuerySet <django.db.models.query.QuerySet.dates>`
|
|
|
|
object containing all months that have objects available according to
|
|
|
|
``queryset``, represented as :class:`datetime.datetime` objects, in
|
2012-06-11 10:34:00 +02:00
|
|
|
ascending order.
|
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
* ``year``: A :class:`~datetime.date` object
|
2012-06-11 10:34:00 +02:00
|
|
|
representing the given year.
|
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
* ``next_year``: A :class:`~datetime.date` object
|
|
|
|
representing the first day of the next year, according to
|
|
|
|
:attr:`~BaseDateListView.allow_empty` and
|
|
|
|
:attr:`~DateMixin.allow_future`.
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
* ``previous_year``: A :class:`~datetime.date` object
|
|
|
|
representing the first day of the previous year, according to
|
|
|
|
:attr:`~BaseDateListView.allow_empty` and
|
|
|
|
:attr:`~DateMixin.allow_future`.
|
2012-06-11 10:34:00 +02:00
|
|
|
|
|
|
|
**Notes**
|
|
|
|
|
|
|
|
* Uses a default ``template_name_suffix`` of ``_archive_year``.
|
|
|
|
|
2013-06-11 22:32:39 +02:00
|
|
|
**Example myapp/views.py**::
|
2012-11-28 01:26:37 +01:00
|
|
|
|
|
|
|
from django.views.generic.dates import YearArchiveView
|
|
|
|
|
|
|
|
from myapp.models import Article
|
|
|
|
|
2023-02-28 20:53:28 +01:00
|
|
|
|
2012-11-28 01:26:37 +01:00
|
|
|
class ArticleYearArchiveView(YearArchiveView):
|
|
|
|
queryset = Article.objects.all()
|
|
|
|
date_field = "pub_date"
|
|
|
|
make_object_list = True
|
|
|
|
allow_future = True
|
|
|
|
|
2013-06-11 22:32:39 +02:00
|
|
|
**Example myapp/urls.py**::
|
2012-11-28 01:26:37 +01:00
|
|
|
|
2016-10-20 19:29:04 +02:00
|
|
|
from django.urls import path
|
2012-11-28 01:26:37 +01:00
|
|
|
|
|
|
|
from myapp.views import ArticleYearArchiveView
|
|
|
|
|
2014-04-02 02:46:34 +02:00
|
|
|
urlpatterns = [
|
2016-10-20 19:29:04 +02:00
|
|
|
path("<int:year>/", ArticleYearArchiveView.as_view(), name="article_year_archive"),
|
2014-04-02 02:46:34 +02:00
|
|
|
]
|
2012-11-28 01:26:37 +01:00
|
|
|
|
|
|
|
**Example myapp/article_archive_year.html**:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
{% for date in date_list %}
|
|
|
|
<li>{{ date|date }}</li>
|
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
<div>
|
|
|
|
<h1>All Articles for {{ year|date:"Y" }}</h1>
|
|
|
|
{% for obj in object_list %}
|
|
|
|
<p>
|
|
|
|
{{ obj.title }} - {{ obj.pub_date|date:"F j, Y" }}
|
|
|
|
</p>
|
|
|
|
{% endfor %}
|
|
|
|
</div>
|
|
|
|
|
2016-01-24 22:26:11 +01:00
|
|
|
``MonthArchiveView``
|
|
|
|
====================
|
2012-08-11 08:07:15 +02:00
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
.. class:: MonthArchiveView
|
2012-06-11 10:34:00 +02:00
|
|
|
|
|
|
|
A monthly archive page showing all objects in a given month. Objects with a
|
|
|
|
date in the *future* are not displayed unless you set ``allow_future`` to
|
|
|
|
``True``.
|
|
|
|
|
|
|
|
**Ancestors (MRO)**
|
|
|
|
|
|
|
|
* :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
|
|
|
|
* :class:`django.views.generic.base.TemplateResponseMixin`
|
|
|
|
* :class:`django.views.generic.dates.BaseMonthArchiveView`
|
|
|
|
* :class:`django.views.generic.dates.YearMixin`
|
|
|
|
* :class:`django.views.generic.dates.MonthMixin`
|
|
|
|
* :class:`django.views.generic.dates.BaseDateListView`
|
|
|
|
* :class:`django.views.generic.list.MultipleObjectMixin`
|
|
|
|
* :class:`django.views.generic.dates.DateMixin`
|
|
|
|
* :class:`django.views.generic.base.View`
|
|
|
|
|
|
|
|
**Context**
|
|
|
|
|
|
|
|
In addition to the context provided by
|
|
|
|
:class:`~django.views.generic.list.MultipleObjectMixin` (via
|
|
|
|
:class:`~django.views.generic.dates.BaseDateListView`), the template's
|
|
|
|
context will be:
|
|
|
|
|
2015-09-11 17:14:35 +02:00
|
|
|
* ``date_list``: A :meth:`QuerySet <django.db.models.query.QuerySet.dates>`
|
|
|
|
object containing all days that have objects available in the given month,
|
|
|
|
according to ``queryset``, represented as :class:`datetime.datetime`
|
|
|
|
objects, in ascending order.
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
* ``month``: A :class:`~datetime.date` object
|
2012-06-11 10:34:00 +02:00
|
|
|
representing the given month.
|
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
* ``next_month``: A :class:`~datetime.date` object
|
|
|
|
representing the first day of the next month, according to
|
|
|
|
:attr:`~BaseDateListView.allow_empty` and
|
|
|
|
:attr:`~DateMixin.allow_future`.
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
* ``previous_month``: A :class:`~datetime.date` object
|
|
|
|
representing the first day of the previous month, according to
|
|
|
|
:attr:`~BaseDateListView.allow_empty` and
|
|
|
|
:attr:`~DateMixin.allow_future`.
|
2012-06-11 10:34:00 +02:00
|
|
|
|
|
|
|
**Notes**
|
|
|
|
|
|
|
|
* Uses a default ``template_name_suffix`` of ``_archive_month``.
|
|
|
|
|
2013-06-11 22:32:39 +02:00
|
|
|
**Example myapp/views.py**::
|
2012-11-28 01:26:37 +01:00
|
|
|
|
|
|
|
from django.views.generic.dates import MonthArchiveView
|
|
|
|
|
|
|
|
from myapp.models import Article
|
|
|
|
|
2023-02-28 20:53:28 +01:00
|
|
|
|
2012-11-28 01:26:37 +01:00
|
|
|
class ArticleMonthArchiveView(MonthArchiveView):
|
|
|
|
queryset = Article.objects.all()
|
|
|
|
date_field = "pub_date"
|
|
|
|
allow_future = True
|
|
|
|
|
2013-06-11 22:32:39 +02:00
|
|
|
**Example myapp/urls.py**::
|
2012-11-28 01:26:37 +01:00
|
|
|
|
2016-10-20 19:29:04 +02:00
|
|
|
from django.urls import path
|
2012-11-28 01:26:37 +01:00
|
|
|
|
|
|
|
from myapp.views import ArticleMonthArchiveView
|
|
|
|
|
2014-04-02 02:46:34 +02:00
|
|
|
urlpatterns = [
|
2012-11-28 01:26:37 +01:00
|
|
|
# Example: /2012/08/
|
2016-10-20 19:29:04 +02:00
|
|
|
path(
|
|
|
|
"<int:year>/<int:month>/",
|
|
|
|
ArticleMonthArchiveView.as_view(month_format="%m"),
|
|
|
|
name="archive_month_numeric",
|
|
|
|
),
|
|
|
|
# Example: /2012/aug/
|
|
|
|
path(
|
|
|
|
"<int:year>/<str:month>/",
|
|
|
|
ArticleMonthArchiveView.as_view(),
|
|
|
|
name="archive_month",
|
|
|
|
),
|
2014-04-02 02:46:34 +02:00
|
|
|
]
|
2012-11-28 01:26:37 +01:00
|
|
|
|
|
|
|
**Example myapp/article_archive_month.html**:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
{% for article in object_list %}
|
|
|
|
<li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
|
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
{% if previous_month %}
|
|
|
|
Previous Month: {{ previous_month|date:"F Y" }}
|
|
|
|
{% endif %}
|
|
|
|
{% if next_month %}
|
|
|
|
Next Month: {{ next_month|date:"F Y" }}
|
|
|
|
{% endif %}
|
|
|
|
</p>
|
|
|
|
|
2016-01-24 22:26:11 +01:00
|
|
|
``WeekArchiveView``
|
|
|
|
===================
|
2012-08-11 08:07:15 +02:00
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
.. class:: WeekArchiveView
|
2012-06-11 10:34:00 +02:00
|
|
|
|
|
|
|
A weekly archive page showing all objects in a given week. Objects with a
|
|
|
|
date in the *future* are not displayed unless you set ``allow_future`` to
|
|
|
|
``True``.
|
|
|
|
|
|
|
|
**Ancestors (MRO)**
|
|
|
|
|
|
|
|
* :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
|
|
|
|
* :class:`django.views.generic.base.TemplateResponseMixin`
|
|
|
|
* :class:`django.views.generic.dates.BaseWeekArchiveView`
|
|
|
|
* :class:`django.views.generic.dates.YearMixin`
|
|
|
|
* :class:`django.views.generic.dates.WeekMixin`
|
|
|
|
* :class:`django.views.generic.dates.BaseDateListView`
|
|
|
|
* :class:`django.views.generic.list.MultipleObjectMixin`
|
|
|
|
* :class:`django.views.generic.dates.DateMixin`
|
|
|
|
* :class:`django.views.generic.base.View`
|
|
|
|
|
|
|
|
**Context**
|
|
|
|
|
|
|
|
In addition to the context provided by
|
|
|
|
:class:`~django.views.generic.list.MultipleObjectMixin` (via
|
|
|
|
:class:`~django.views.generic.dates.BaseDateListView`), the template's
|
|
|
|
context will be:
|
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
* ``week``: A :class:`~datetime.date` object
|
2012-06-11 10:34:00 +02:00
|
|
|
representing the first day of the given week.
|
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
* ``next_week``: A :class:`~datetime.date` object
|
|
|
|
representing the first day of the next week, according to
|
|
|
|
:attr:`~BaseDateListView.allow_empty` and
|
|
|
|
:attr:`~DateMixin.allow_future`.
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
* ``previous_week``: A :class:`~datetime.date` object
|
|
|
|
representing the first day of the previous week, according to
|
|
|
|
:attr:`~BaseDateListView.allow_empty` and
|
|
|
|
:attr:`~DateMixin.allow_future`.
|
2012-06-11 10:34:00 +02:00
|
|
|
|
|
|
|
**Notes**
|
|
|
|
|
|
|
|
* Uses a default ``template_name_suffix`` of ``_archive_week``.
|
|
|
|
|
2016-02-20 18:04:50 +01:00
|
|
|
* The ``week_format`` attribute is a :func:`~time.strptime` format string
|
|
|
|
used to parse the week number. The following values are supported:
|
|
|
|
|
|
|
|
* ``'%U'``: Based on the United States week system where the week
|
|
|
|
begins on Sunday. This is the default value.
|
|
|
|
|
2016-03-06 00:47:26 +01:00
|
|
|
* ``'%W'``: Similar to ``'%U'``, except it assumes that the week
|
2016-02-20 18:04:50 +01:00
|
|
|
begins on Monday. This is not the same as the ISO 8601 week number.
|
|
|
|
|
2020-06-25 23:00:51 +02:00
|
|
|
* ``'%V'``: ISO 8601 week number where the week begins on Monday.
|
|
|
|
|
2013-06-11 22:32:39 +02:00
|
|
|
**Example myapp/views.py**::
|
2012-11-28 01:26:37 +01:00
|
|
|
|
|
|
|
from django.views.generic.dates import WeekArchiveView
|
|
|
|
|
|
|
|
from myapp.models import Article
|
|
|
|
|
2023-02-28 20:53:28 +01:00
|
|
|
|
2012-11-28 01:26:37 +01:00
|
|
|
class ArticleWeekArchiveView(WeekArchiveView):
|
|
|
|
queryset = Article.objects.all()
|
|
|
|
date_field = "pub_date"
|
|
|
|
week_format = "%W"
|
|
|
|
allow_future = True
|
|
|
|
|
2013-06-11 22:32:39 +02:00
|
|
|
**Example myapp/urls.py**::
|
2012-11-28 01:26:37 +01:00
|
|
|
|
2016-10-20 19:29:04 +02:00
|
|
|
from django.urls import path
|
2012-11-28 01:26:37 +01:00
|
|
|
|
|
|
|
from myapp.views import ArticleWeekArchiveView
|
|
|
|
|
2014-04-02 02:46:34 +02:00
|
|
|
urlpatterns = [
|
2012-11-28 01:26:37 +01:00
|
|
|
# Example: /2012/week/23/
|
2016-10-20 19:29:04 +02:00
|
|
|
path(
|
|
|
|
"<int:year>/week/<int:week>/",
|
|
|
|
ArticleWeekArchiveView.as_view(),
|
|
|
|
name="archive_week",
|
|
|
|
),
|
2014-04-02 02:46:34 +02:00
|
|
|
]
|
2012-11-28 01:26:37 +01:00
|
|
|
|
|
|
|
**Example myapp/article_archive_week.html**:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
<h1>Week {{ week|date:'W' }}</h1>
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
{% for article in object_list %}
|
|
|
|
<li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
|
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
{% if previous_week %}
|
2016-02-20 18:04:50 +01:00
|
|
|
Previous Week: {{ previous_week|date:"W" }} of year {{ previous_week|date:"Y" }}
|
2012-11-28 01:26:37 +01:00
|
|
|
{% endif %}
|
|
|
|
{% if previous_week and next_week %}--{% endif %}
|
|
|
|
{% if next_week %}
|
2016-02-20 18:04:50 +01:00
|
|
|
Next week: {{ next_week|date:"W" }} of year {{ next_week|date:"Y" }}
|
2012-11-28 01:26:37 +01:00
|
|
|
{% endif %}
|
|
|
|
</p>
|
|
|
|
|
2016-02-20 18:04:50 +01:00
|
|
|
In this example, you are outputting the week number. Keep in mind that week
|
|
|
|
numbers computed by the :tfilter:`date` template filter with the ``'W'``
|
|
|
|
format character are not always the same as those computed by
|
|
|
|
:func:`~time.strftime` and :func:`~time.strptime` with the ``'%W'`` format
|
|
|
|
string. For year 2015, for example, week numbers output by :tfilter:`date`
|
|
|
|
are higher by one compared to those output by :func:`~time.strftime`. There
|
|
|
|
isn't an equivalent for the ``'%U'`` :func:`~time.strftime` format string
|
|
|
|
in :tfilter:`date`. Therefore, you should avoid using :tfilter:`date` to
|
|
|
|
generate URLs for ``WeekArchiveView``.
|
2012-11-28 01:26:37 +01:00
|
|
|
|
2016-01-24 22:26:11 +01:00
|
|
|
``DayArchiveView``
|
|
|
|
==================
|
2012-08-11 08:07:15 +02:00
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
.. class:: DayArchiveView
|
2012-06-11 10:34:00 +02:00
|
|
|
|
|
|
|
A day archive page showing all objects in a given day. Days in the future
|
|
|
|
throw a 404 error, regardless of whether any objects exist for future days,
|
|
|
|
unless you set ``allow_future`` to ``True``.
|
|
|
|
|
|
|
|
**Ancestors (MRO)**
|
|
|
|
|
|
|
|
* :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
|
|
|
|
* :class:`django.views.generic.base.TemplateResponseMixin`
|
|
|
|
* :class:`django.views.generic.dates.BaseDayArchiveView`
|
|
|
|
* :class:`django.views.generic.dates.YearMixin`
|
|
|
|
* :class:`django.views.generic.dates.MonthMixin`
|
|
|
|
* :class:`django.views.generic.dates.DayMixin`
|
|
|
|
* :class:`django.views.generic.dates.BaseDateListView`
|
|
|
|
* :class:`django.views.generic.list.MultipleObjectMixin`
|
|
|
|
* :class:`django.views.generic.dates.DateMixin`
|
|
|
|
* :class:`django.views.generic.base.View`
|
|
|
|
|
|
|
|
**Context**
|
|
|
|
|
|
|
|
In addition to the context provided by
|
|
|
|
:class:`~django.views.generic.list.MultipleObjectMixin` (via
|
|
|
|
:class:`~django.views.generic.dates.BaseDateListView`), the template's
|
|
|
|
context will be:
|
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
* ``day``: A :class:`~datetime.date` object
|
2012-06-11 10:34:00 +02:00
|
|
|
representing the given day.
|
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
* ``next_day``: A :class:`~datetime.date` object
|
|
|
|
representing the next day, according to
|
|
|
|
:attr:`~BaseDateListView.allow_empty` and
|
|
|
|
:attr:`~DateMixin.allow_future`.
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
* ``previous_day``: A :class:`~datetime.date` object
|
|
|
|
representing the previous day, according to
|
|
|
|
:attr:`~BaseDateListView.allow_empty` and
|
|
|
|
:attr:`~DateMixin.allow_future`.
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
* ``next_month``: A :class:`~datetime.date` object
|
|
|
|
representing the first day of the next month, according to
|
|
|
|
:attr:`~BaseDateListView.allow_empty` and
|
|
|
|
:attr:`~DateMixin.allow_future`.
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
* ``previous_month``: A :class:`~datetime.date` object
|
|
|
|
representing the first day of the previous month, according to
|
|
|
|
:attr:`~BaseDateListView.allow_empty` and
|
|
|
|
:attr:`~DateMixin.allow_future`.
|
2012-06-11 10:34:00 +02:00
|
|
|
|
|
|
|
**Notes**
|
|
|
|
|
|
|
|
* Uses a default ``template_name_suffix`` of ``_archive_day``.
|
|
|
|
|
2013-06-11 22:32:39 +02:00
|
|
|
**Example myapp/views.py**::
|
2012-11-28 01:26:37 +01:00
|
|
|
|
|
|
|
from django.views.generic.dates import DayArchiveView
|
|
|
|
|
|
|
|
from myapp.models import Article
|
|
|
|
|
2023-02-28 20:53:28 +01:00
|
|
|
|
2012-11-28 01:26:37 +01:00
|
|
|
class ArticleDayArchiveView(DayArchiveView):
|
|
|
|
queryset = Article.objects.all()
|
|
|
|
date_field = "pub_date"
|
|
|
|
allow_future = True
|
|
|
|
|
2013-06-11 22:32:39 +02:00
|
|
|
**Example myapp/urls.py**::
|
2012-11-28 01:26:37 +01:00
|
|
|
|
2016-10-20 19:29:04 +02:00
|
|
|
from django.urls import path
|
2012-11-28 01:26:37 +01:00
|
|
|
|
|
|
|
from myapp.views import ArticleDayArchiveView
|
|
|
|
|
2014-04-02 02:46:34 +02:00
|
|
|
urlpatterns = [
|
2012-11-28 01:26:37 +01:00
|
|
|
# Example: /2012/nov/10/
|
2016-10-20 19:29:04 +02:00
|
|
|
path(
|
|
|
|
"<int:year>/<str:month>/<int:day>/",
|
|
|
|
ArticleDayArchiveView.as_view(),
|
|
|
|
name="archive_day",
|
|
|
|
),
|
2014-04-02 02:46:34 +02:00
|
|
|
]
|
2012-11-28 01:26:37 +01:00
|
|
|
|
|
|
|
**Example myapp/article_archive_day.html**:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
<h1>{{ day }}</h1>
|
|
|
|
|
|
|
|
<ul>
|
|
|
|
{% for article in object_list %}
|
|
|
|
<li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
|
|
|
|
{% endfor %}
|
|
|
|
</ul>
|
|
|
|
|
|
|
|
<p>
|
|
|
|
{% if previous_day %}
|
|
|
|
Previous Day: {{ previous_day }}
|
|
|
|
{% endif %}
|
|
|
|
{% if previous_day and next_day %}--{% endif %}
|
|
|
|
{% if next_day %}
|
|
|
|
Next Day: {{ next_day }}
|
|
|
|
{% endif %}
|
|
|
|
</p>
|
|
|
|
|
2016-01-24 22:26:11 +01:00
|
|
|
``TodayArchiveView``
|
|
|
|
====================
|
2012-08-11 08:07:15 +02:00
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
.. class:: TodayArchiveView
|
2012-06-11 10:34:00 +02:00
|
|
|
|
|
|
|
A day archive page showing all objects for *today*. This is exactly the
|
|
|
|
same as :class:`django.views.generic.dates.DayArchiveView`, except today's
|
|
|
|
date is used instead of the ``year``/``month``/``day`` arguments.
|
|
|
|
|
|
|
|
**Ancestors (MRO)**
|
|
|
|
|
|
|
|
* :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
|
|
|
|
* :class:`django.views.generic.base.TemplateResponseMixin`
|
|
|
|
* :class:`django.views.generic.dates.BaseTodayArchiveView`
|
|
|
|
* :class:`django.views.generic.dates.BaseDayArchiveView`
|
|
|
|
* :class:`django.views.generic.dates.YearMixin`
|
|
|
|
* :class:`django.views.generic.dates.MonthMixin`
|
|
|
|
* :class:`django.views.generic.dates.DayMixin`
|
|
|
|
* :class:`django.views.generic.dates.BaseDateListView`
|
|
|
|
* :class:`django.views.generic.list.MultipleObjectMixin`
|
|
|
|
* :class:`django.views.generic.dates.DateMixin`
|
|
|
|
* :class:`django.views.generic.base.View`
|
|
|
|
|
2012-11-28 01:26:37 +01:00
|
|
|
**Notes**
|
|
|
|
|
|
|
|
* Uses a default ``template_name_suffix`` of ``_archive_today``.
|
|
|
|
|
2013-06-11 22:32:39 +02:00
|
|
|
**Example myapp/views.py**::
|
2012-11-28 01:26:37 +01:00
|
|
|
|
|
|
|
from django.views.generic.dates import TodayArchiveView
|
|
|
|
|
|
|
|
from myapp.models import Article
|
|
|
|
|
2023-02-28 20:53:28 +01:00
|
|
|
|
2012-11-28 01:26:37 +01:00
|
|
|
class ArticleTodayArchiveView(TodayArchiveView):
|
|
|
|
queryset = Article.objects.all()
|
|
|
|
date_field = "pub_date"
|
|
|
|
allow_future = True
|
|
|
|
|
2013-06-11 22:32:39 +02:00
|
|
|
**Example myapp/urls.py**::
|
2012-11-28 01:26:37 +01:00
|
|
|
|
2016-10-20 19:29:04 +02:00
|
|
|
from django.urls import path
|
2012-11-28 01:26:37 +01:00
|
|
|
|
|
|
|
from myapp.views import ArticleTodayArchiveView
|
|
|
|
|
2014-04-02 02:46:34 +02:00
|
|
|
urlpatterns = [
|
2016-10-20 19:29:04 +02:00
|
|
|
path("today/", ArticleTodayArchiveView.as_view(), name="archive_today"),
|
2014-04-02 02:46:34 +02:00
|
|
|
]
|
2012-11-28 01:26:37 +01:00
|
|
|
|
|
|
|
.. admonition:: Where is the example template for ``TodayArchiveView``?
|
|
|
|
|
|
|
|
This view uses by default the same template as the
|
|
|
|
:class:`~DayArchiveView`, which is in the previous example. If you need
|
|
|
|
a different template, set the ``template_name`` attribute to be the
|
|
|
|
name of the new template.
|
2012-08-11 08:07:15 +02:00
|
|
|
|
2016-01-24 22:26:11 +01:00
|
|
|
``DateDetailView``
|
|
|
|
==================
|
2012-08-11 08:07:15 +02:00
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
.. class:: DateDetailView
|
2012-06-11 10:34:00 +02:00
|
|
|
|
|
|
|
A page representing an individual object. If the object has a date value in
|
|
|
|
the future, the view will throw a 404 error by default, unless you set
|
|
|
|
``allow_future`` to ``True``.
|
|
|
|
|
|
|
|
**Ancestors (MRO)**
|
|
|
|
|
|
|
|
* :class:`django.views.generic.detail.SingleObjectTemplateResponseMixin`
|
|
|
|
* :class:`django.views.generic.base.TemplateResponseMixin`
|
|
|
|
* :class:`django.views.generic.dates.BaseDateDetailView`
|
|
|
|
* :class:`django.views.generic.dates.YearMixin`
|
|
|
|
* :class:`django.views.generic.dates.MonthMixin`
|
|
|
|
* :class:`django.views.generic.dates.DayMixin`
|
|
|
|
* :class:`django.views.generic.dates.DateMixin`
|
2021-02-03 15:25:56 +01:00
|
|
|
* :class:`django.views.generic.detail.BaseDetailView`
|
2012-06-11 10:34:00 +02:00
|
|
|
* :class:`django.views.generic.detail.SingleObjectMixin`
|
|
|
|
* :class:`django.views.generic.base.View`
|
|
|
|
|
2012-11-28 01:26:37 +01:00
|
|
|
**Context**
|
|
|
|
|
|
|
|
* Includes the single object associated with the ``model`` specified in
|
|
|
|
the ``DateDetailView``.
|
|
|
|
|
|
|
|
**Notes**
|
|
|
|
|
|
|
|
* Uses a default ``template_name_suffix`` of ``_detail``.
|
|
|
|
|
2013-06-11 22:32:39 +02:00
|
|
|
**Example myapp/urls.py**::
|
2012-11-28 01:26:37 +01:00
|
|
|
|
2016-10-20 19:29:04 +02:00
|
|
|
from django.urls import path
|
2012-11-28 01:26:37 +01:00
|
|
|
from django.views.generic.dates import DateDetailView
|
|
|
|
|
2014-04-02 02:46:34 +02:00
|
|
|
urlpatterns = [
|
2016-10-20 19:29:04 +02:00
|
|
|
path(
|
|
|
|
"<int:year>/<str:month>/<int:day>/<int:pk>/",
|
|
|
|
DateDetailView.as_view(model=Article, date_field="pub_date"),
|
|
|
|
name="archive_date_detail",
|
|
|
|
),
|
2014-04-02 02:46:34 +02:00
|
|
|
]
|
2012-11-28 01:26:37 +01:00
|
|
|
|
|
|
|
**Example myapp/article_detail.html**:
|
|
|
|
|
|
|
|
.. code-block:: html+django
|
|
|
|
|
|
|
|
<h1>{{ object.title }}</h1>
|
|
|
|
|
2012-06-11 10:34:00 +02:00
|
|
|
.. note::
|
|
|
|
|
2012-09-08 18:20:20 +02:00
|
|
|
All of the generic views listed above have matching ``Base`` views that
|
2013-11-06 10:47:07 +01:00
|
|
|
only differ in that they do not include the
|
2013-11-16 05:03:01 +01:00
|
|
|
:class:`~django.views.generic.list.MultipleObjectTemplateResponseMixin`
|
|
|
|
(for the archive views) or
|
|
|
|
:class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`
|
|
|
|
(for the :class:`DateDetailView`):
|
2012-09-08 18:20:20 +02:00
|
|
|
|
|
|
|
.. class:: BaseArchiveIndexView
|
|
|
|
|
|
|
|
.. class:: BaseYearArchiveView
|
|
|
|
|
|
|
|
.. class:: BaseMonthArchiveView
|
|
|
|
|
|
|
|
.. class:: BaseWeekArchiveView
|
|
|
|
|
|
|
|
.. class:: BaseDayArchiveView
|
|
|
|
|
|
|
|
.. class:: BaseTodayArchiveView
|
|
|
|
|
|
|
|
.. class:: BaseDateDetailView
|