Generic date views

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

Note

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

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

ArchiveIndexView

  • class ArchiveIndexView[source]
  • A top-level index page showing the "latest" objects, by date. Objects witha date in the future are not included unless you set allow_future toTrue.

Ancestors (MRO)

In addition to the context provided bydjango.views.generic.list.MultipleObjectMixin (viadjango.views.generic.dates.BaseDateListView), the template'scontext will be:

  • date_list: A QuerySetobject containing all years that have objects available according toqueryset, represented as datetime.datetime objects, indescending 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 tomonth or day using the attribute date_list_period. This also appliesto all subclass views.Example myapp/urls.py:
  1. from django.urls import path
  2. from django.views.generic.dates import ArchiveIndexView
  3.  
  4. from myapp.models import Article
  5.  
  6. urlpatterns = [
  7. path('archive/',
  8. ArchiveIndexView.as_view(model=Article, date_field="pub_date"),
  9. name="article_archive"),
  10. ]

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[source]
  • A yearly archive page showing all available months in a given year. Objectswith a date in the future are not displayed unless you setallow_future to True.

Ancestors (MRO)

Context

In addition to the context provided bydjango.views.generic.list.MultipleObjectMixin (viadjango.views.generic.dates.BaseDateListView), the template'scontext will be:

  • date_list: A QuerySetobject containing all months that have objects available according toqueryset, represented as datetime.datetime objects, inascending order.
  • year: A date objectrepresenting the given year.
  • next_year: A date objectrepresenting the first day of the next year, according toallow_empty andallow_future.
  • previous_year: A date objectrepresenting the first day of the previous year, according toallow_empty andallow_future.Notes

  • Uses a default template_name_suffix of _archive_year.Example myapp/views.py:

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

Example myapp/urls.py:

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

Example myapp/article_archive_year.html:

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

MonthArchiveView

  • class MonthArchiveView[source]
  • A monthly archive page showing all objects in a given month. Objects with adate in the future are not displayed unless you set allow_future toTrue.

Ancestors (MRO)

In addition to the context provided byMultipleObjectMixin (viaBaseDateListView), the template'scontext will be:

  • date_list: A QuerySetobject containing all days that have objects available in the given month,according to queryset, represented as datetime.datetimeobjects, in ascending order.
  • month: A date objectrepresenting the given month.
  • next_month: A date objectrepresenting the first day of the next month, according toallow_empty andallow_future.
  • previous_month: A date objectrepresenting the first day of the previous month, according toallow_empty andallow_future.Notes

  • Uses a default template_name_suffix of _archive_month.Example myapp/views.py:

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

Example myapp/urls.py:

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

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.  
  7. <p>
  8. {% if previous_month %}
  9. Previous Month: {{ previous_month|date:"F Y" }}
  10. {% endif %}
  11. {% if next_month %}
  12. Next Month: {{ next_month|date:"F Y" }}
  13. {% endif %}
  14. </p>

WeekArchiveView

  • class WeekArchiveView[source]
  • A weekly archive page showing all objects in a given week. Objects with adate in the future are not displayed unless you set allow_future toTrue.

Ancestors (MRO)

In addition to the context provided byMultipleObjectMixin (viaBaseDateListView), the template'scontext will be:

  • week: A date objectrepresenting the first day of the given week.
  • next_week: A date objectrepresenting the first day of the next week, according toallow_empty andallow_future.
  • previous_week: A date objectrepresenting the first day of the previous week, according toallow_empty andallow_future.Notes

  • Uses a default template_name_suffix of _archive_week.

  • The week_format attribute is a strptime() format stringused to parse the week number. The following values are supported:
    • '%U': Based on the United States week system where the weekbegins on Sunday. This is the default value.
    • '%W': Similar to '%U', except it assumes that the weekbegins on Monday. This is not the same as the ISO 8601 week number.Example myapp/views.py:
  1. from django.views.generic.dates import WeekArchiveView
  2.  
  3. from myapp.models import Article
  4.  
  5. class ArticleWeekArchiveView(WeekArchiveView):
  6. queryset = Article.objects.all()
  7. date_field = "pub_date"
  8. week_format = "%W"
  9. allow_future = True

Example myapp/urls.py:

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

Example myapp/article_archive_week.html:

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

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

DayArchiveView

  • class DayArchiveView[source]
  • A day archive page showing all objects in a given day. Days in the futurethrow a 404 error, regardless of whether any objects exist for future days,unless you set allow_future to True.

Ancestors (MRO)

In addition to the context provided byMultipleObjectMixin (viaBaseDateListView), the template'scontext will be:

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

Example myapp/urls.py:

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

Example myapp/article_archive_day.html:

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

TodayArchiveView

Ancestors (MRO)

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

Example myapp/urls.py:

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

Where is the example template for TodayArchiveView?

This view uses by default the same template as theDayArchiveView, which is in the previous example. If you needa different template, set the template_name attribute to be thename of the new template.

DateDetailView

  • class DateDetailView[source]
  • A page representing an individual object. If the object has a date value inthe future, the view will throw a 404 error by default, unless you setallow_future to True.

Ancestors (MRO)

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

Example myapp/article_detail.html:

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

Note

All of the generic views listed above have matching Base views thatonly differ in that they do not include theMultipleObjectTemplateResponseMixin(for the archive views) orSingleObjectTemplateResponseMixin(for the DateDetailView):