Generic date views

Date-based generic views, provided in django.views.generic.dates, are views for displaying drilldown pages for date-based data.

Note

Some of the examples on this page assume that an Article model has been defined as follows in myapp/models.py:

  1. from django.db import models
  2. from django.urls import reverse
  3. class Article(models.Model):
  4. title = models.CharField(max_length=200)
  5. pub_date = models.DateField()
  6. def get_absolute_url(self):
  7. return reverse('article-detail', kwargs={'pk': self.pk})

ArchiveIndexView

class ArchiveIndexView

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)

Context

In addition to the context provided by django.views.generic.list.MultipleObjectMixin (via django.views.generic.dates.BaseDateListView), the template’s context will be:

  • date_list: A QuerySet object containing all years that have objects available according to queryset, represented as datetime.datetime objects, in descending order.

Notes

  • Uses a default context_object_name of latest.
  • Uses a default template_name_suffix of _archive.
  • 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.

Example myapp/urls.py:

  1. from django.urls import path
  2. from django.views.generic.dates import ArchiveIndexView
  3. from myapp.models import Article
  4. urlpatterns = [
  5. path('archive/',
  6. ArchiveIndexView.as_view(model=Article, date_field="pub_date"),
  7. name="article_archive"),
  8. ]

Example myapp/article_archive.html:

  1. <ul>
  2. {% for article in latest %}
  3. <li>{{ article.pub_date }}: {{ article.title }}</li>
  4. {% endfor %}
  5. </ul>

This will output all articles.

YearArchiveView

class YearArchiveView

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)

Context

In addition to the context provided by django.views.generic.list.MultipleObjectMixin (via django.views.generic.dates.BaseDateListView), the template’s context will be:

  • date_list: A QuerySet object containing all months that have objects available according to queryset, represented as datetime.datetime objects, in ascending order.
  • year: A date object representing the given year.
  • next_year: A date object representing the first day of the next year, according to allow_empty and allow_future.
  • previous_year: A date object representing the first day of the previous year, according to allow_empty and allow_future.

Notes

  • Uses a default template_name_suffix of _archive_year.

Example myapp/views.py:

  1. from django.views.generic.dates import YearArchiveView
  2. from myapp.models import Article
  3. class ArticleYearArchiveView(YearArchiveView):
  4. queryset = Article.objects.all()
  5. date_field = "pub_date"
  6. make_object_list = True
  7. allow_future = True

Example myapp/urls.py:

  1. from django.urls import path
  2. from myapp.views import ArticleYearArchiveView
  3. urlpatterns = [
  4. path('<int:year>/',
  5. ArticleYearArchiveView.as_view(),
  6. name="article_year_archive"),
  7. ]

Example myapp/article_archive_year.html:

  1. <ul>
  2. {% for date in date_list %}
  3. <li>{{ date|date }}</li>
  4. {% endfor %}
  5. </ul>
  6. <div>
  7. <h1>All Articles for {{ year|date:"Y" }}</h1>
  8. {% for obj in object_list %}
  9. <p>
  10. {{ obj.title }} - {{ obj.pub_date|date:"F j, Y" }}
  11. </p>
  12. {% endfor %}
  13. </div>

MonthArchiveView

class MonthArchiveView

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)

Context

In addition to the context provided by MultipleObjectMixin (via BaseDateListView), the template’s context will be:

  • date_list: A QuerySet object containing all days that have objects available in the given month, according to queryset, represented as datetime.datetime objects, in ascending order.
  • month: A date object representing the given month.
  • next_month: A date object representing the first day of the next month, according to allow_empty and allow_future.
  • previous_month: A date object representing the first day of the previous month, according to allow_empty and allow_future.

Notes

  • Uses a default template_name_suffix of _archive_month.

Example myapp/views.py:

  1. from django.views.generic.dates import MonthArchiveView
  2. from myapp.models import Article
  3. class ArticleMonthArchiveView(MonthArchiveView):
  4. queryset = Article.objects.all()
  5. date_field = "pub_date"
  6. allow_future = True

Example myapp/urls.py:

  1. from django.urls import path
  2. from myapp.views import ArticleMonthArchiveView
  3. urlpatterns = [
  4. # Example: /2012/08/
  5. path('<int:year>/<int:month>/',
  6. ArticleMonthArchiveView.as_view(month_format='%m'),
  7. name="archive_month_numeric"),
  8. # Example: /2012/aug/
  9. path('<int:year>/<str:month>/',
  10. ArticleMonthArchiveView.as_view(),
  11. name="archive_month"),
  12. ]

Example myapp/article_archive_month.html:

  1. <ul>
  2. {% for article in object_list %}
  3. <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
  4. {% endfor %}
  5. </ul>
  6. <p>
  7. {% if previous_month %}
  8. Previous Month: {{ previous_month|date:"F Y" }}
  9. {% endif %}
  10. {% if next_month %}
  11. Next Month: {{ next_month|date:"F Y" }}
  12. {% endif %}
  13. </p>

WeekArchiveView

class WeekArchiveView

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)

Context

In addition to the context provided by MultipleObjectMixin (via BaseDateListView), the template’s context will be:

  • week: A date object representing the first day of the given week.
  • next_week: A date object representing the first day of the next week, according to allow_empty and allow_future.
  • previous_week: A date object representing the first day of the previous week, according to allow_empty and allow_future.

Notes

  • Uses a default template_name_suffix of _archive_week.

  • The week_format attribute is a 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.

    • '%W': Similar to '%U', except it assumes that the week begins on Monday. This is not the same as the ISO 8601 week number.

    • '%V': ISO 8601 week number where the week begins on Monday.

      New in Django Development version:

      Support for the '%V' week format was added.

Example myapp/views.py:

  1. from django.views.generic.dates import WeekArchiveView
  2. from myapp.models import Article
  3. class ArticleWeekArchiveView(WeekArchiveView):
  4. queryset = Article.objects.all()
  5. date_field = "pub_date"
  6. week_format = "%W"
  7. allow_future = True

Example myapp/urls.py:

  1. from django.urls import path
  2. from myapp.views import ArticleWeekArchiveView
  3. urlpatterns = [
  4. # Example: /2012/week/23/
  5. path('<int:year>/week/<int:week>/',
  6. ArticleWeekArchiveView.as_view(),
  7. name="archive_week"),
  8. ]

Example myapp/article_archive_week.html:

  1. <h1>Week {{ week|date:'W' }}</h1>
  2. <ul>
  3. {% for article in object_list %}
  4. <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
  5. {% endfor %}
  6. </ul>
  7. <p>
  8. {% if previous_week %}
  9. Previous Week: {{ previous_week|date:"W" }} of year {{ previous_week|date:"Y" }}
  10. {% endif %}
  11. {% if previous_week and next_week %}--{% endif %}
  12. {% if next_week %}
  13. Next week: {{ next_week|date:"W" }} of year {{ next_week|date:"Y" }}
  14. {% endif %}
  15. </p>

In this example, you are outputting the week number. Keep in mind that week numbers computed by the date template filter with the 'W' format character are not always the same as those computed by strftime() and strptime() with the '%W' format string. For year 2015, for example, week numbers output by date are higher by one compared to those output by strftime(). There isn’t an equivalent for the '%U' strftime() format string in date. Therefore, you should avoid using date to generate URLs for WeekArchiveView.

DayArchiveView

class DayArchiveView

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)

Context

In addition to the context provided by MultipleObjectMixin (via BaseDateListView), the template’s context will be:

Notes

  • Uses a default template_name_suffix of _archive_day.

Example myapp/views.py:

  1. from django.views.generic.dates import DayArchiveView
  2. from myapp.models import Article
  3. class ArticleDayArchiveView(DayArchiveView):
  4. queryset = Article.objects.all()
  5. date_field = "pub_date"
  6. allow_future = True

Example myapp/urls.py:

  1. from django.urls import path
  2. from myapp.views import ArticleDayArchiveView
  3. urlpatterns = [
  4. # Example: /2012/nov/10/
  5. path('<int:year>/<str:month>/<int:day>/',
  6. ArticleDayArchiveView.as_view(),
  7. name="archive_day"),
  8. ]

Example myapp/article_archive_day.html:

  1. <h1>{{ day }}</h1>
  2. <ul>
  3. {% for article in object_list %}
  4. <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
  5. {% endfor %}
  6. </ul>
  7. <p>
  8. {% if previous_day %}
  9. Previous Day: {{ previous_day }}
  10. {% endif %}
  11. {% if previous_day and next_day %}--{% endif %}
  12. {% if next_day %}
  13. Next Day: {{ next_day }}
  14. {% endif %}
  15. </p>

TodayArchiveView

class TodayArchiveView

A day archive page showing all objects for today. This is exactly the same as django.views.generic.dates.DayArchiveView, except today’s date is used instead of the year/month/day arguments.

Ancestors (MRO)

Notes

  • Uses a default template_name_suffix of _archive_today.

Example myapp/views.py:

  1. from django.views.generic.dates import TodayArchiveView
  2. from myapp.models import Article
  3. class ArticleTodayArchiveView(TodayArchiveView):
  4. queryset = Article.objects.all()
  5. date_field = "pub_date"
  6. allow_future = True

Example myapp/urls.py:

  1. from django.urls import path
  2. from myapp.views import ArticleTodayArchiveView
  3. urlpatterns = [
  4. path('today/',
  5. ArticleTodayArchiveView.as_view(),
  6. name="archive_today"),
  7. ]

Where is the example template for TodayArchiveView?

This view uses by default the same template as the 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.

DateDetailView

class DateDetailView

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)

Context

  • Includes the single object associated with the model specified in the DateDetailView.

Notes

  • Uses a default template_name_suffix of _detail.

Example myapp/urls.py:

  1. from django.urls import path
  2. from django.views.generic.dates import DateDetailView
  3. urlpatterns = [
  4. path('<int:year>/<str:month>/<int:day>/<int:pk>/',
  5. DateDetailView.as_view(model=Article, date_field="pub_date"),
  6. name="archive_date_detail"),
  7. ]

Example myapp/article_detail.html:

  1. <h1>{{ object.title }}</h1>

Note

All of the generic views listed above have matching Base views that only differ in that they do not include the MultipleObjectTemplateResponseMixin (for the archive views) or SingleObjectTemplateResponseMixin (for the DateDetailView):

class BaseArchiveIndexView

class BaseYearArchiveView

class BaseMonthArchiveView

class BaseWeekArchiveView

class BaseDayArchiveView

class BaseTodayArchiveView

class BaseDateDetailView