Generic date views
Date-based generic views, provided in django.views.generic.dates
, areviews for displaying drilldown pages for date-based data.
注解
Some of the examples on this page assume that an Article
model has beendefined as follows in myapp/models.py
:
- from django.db import models
- from django.urls import reverse
- 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})
ArchiveIndexView
- class
ArchiveIndexView
[源代码] - 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)
django.views.generic.list.MultipleObjectTemplateResponseMixin
django.views.generic.base.TemplateResponseMixin
django.views.generic.dates.BaseArchiveIndexView
django.views.generic.dates.BaseDateListView
django.views.generic.list.MultipleObjectMixin
django.views.generic.dates.DateMixin
django.views.generic.base.View
Context
In addition to the context provided bydjango.views.generic.list.MultipleObjectMixin
(viadjango.views.generic.dates.BaseDateListView
), the template'scontext will be:
date_list
: AQuerySet
object containing all years that have objects available according toqueryset
, represented asdatetime.datetime
objects, indescending order.
NotesUses a default
context_object_name
oflatest
.- 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 attributedate_list_period
. This also appliesto all subclass views.
Example myapp/urls.py:
- from django.urls import path
- from django.views.generic.dates import ArchiveIndexView
- from myapp.models import Article
- urlpatterns = [
- path('archive/',
- ArchiveIndexView.as_view(model=Article, date_field="pub_date"),
- name="article_archive"),
- ]
Example myapp/article_archive.html:
- <ul>
- {% for article in latest %}
- <li>{{ article.pub_date }}: {{ article.title }}</li>
- {% endfor %}
- </ul>
This will output all articles.
YearArchiveView
- class
YearArchiveView
[源代码] - A yearly archive page showing all available months in a given year. Objectswith a date in the future are not displayed unless you set
allow_future
toTrue
.
Ancestors (MRO)
django.views.generic.list.MultipleObjectTemplateResponseMixin
django.views.generic.base.TemplateResponseMixin
django.views.generic.dates.BaseYearArchiveView
django.views.generic.dates.YearMixin
django.views.generic.dates.BaseDateListView
django.views.generic.list.MultipleObjectMixin
django.views.generic.dates.DateMixin
django.views.generic.base.View
make_object_list
A boolean specifying whether to retrieve the full list of objects forthis year and pass those to the template. If
True
, the list ofobjects will be made available to the context. IfFalse
, theNone
queryset will be used as the object list. By default, this isFalse
.- Determine if an object list will be returned as part of the context.Returns
make_object_list
by default.
Context
In addition to the context provided bydjango.views.generic.list.MultipleObjectMixin
(viadjango.views.generic.dates.BaseDateListView
), the template'scontext will be:
date_list
: AQuerySet
object containing all months that have objects available according toqueryset
, represented asdatetime.datetime
objects, inascending order.year
: Adate
objectrepresenting the given year.next_year
: Adate
objectrepresenting the first day of the next year, according toallow_empty
andallow_future
.previous_year
: Adate
objectrepresenting the first day of the previous year, according toallow_empty
andallow_future
.
NotesUses a default
template_name_suffix
of_archive_year
.
Example myapp/views.py:
- from django.views.generic.dates import YearArchiveView
- from myapp.models import Article
- class ArticleYearArchiveView(YearArchiveView):
- queryset = Article.objects.all()
- date_field = "pub_date"
- make_object_list = True
- allow_future = True
Example myapp/urls.py:
- from django.urls import path
- from myapp.views import ArticleYearArchiveView
- urlpatterns = [
- path('<int:year>/',
- ArticleYearArchiveView.as_view(),
- name="article_year_archive"),
- ]
Example myapp/article_archive_year.html:
- <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>
MonthArchiveView
- class
MonthArchiveView
[源代码] - 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)
django.views.generic.list.MultipleObjectTemplateResponseMixin
django.views.generic.base.TemplateResponseMixin
django.views.generic.dates.BaseMonthArchiveView
django.views.generic.dates.YearMixin
django.views.generic.dates.MonthMixin
django.views.generic.dates.BaseDateListView
django.views.generic.list.MultipleObjectMixin
django.views.generic.dates.DateMixin
django.views.generic.base.View
Context
In addition to the context provided byMultipleObjectMixin
(viaBaseDateListView
), the template'scontext will be:
date_list
: AQuerySet
object containing all days that have objects available in the given month,according toqueryset
, represented asdatetime.datetime
objects, in ascending order.month
: Adate
objectrepresenting the given month.next_month
: Adate
objectrepresenting the first day of the next month, according toallow_empty
andallow_future
.previous_month
: Adate
objectrepresenting the first day of the previous month, according toallow_empty
andallow_future
.
NotesUses a default
template_name_suffix
of_archive_month
.
Example myapp/views.py:
- from django.views.generic.dates import MonthArchiveView
- from myapp.models import Article
- class ArticleMonthArchiveView(MonthArchiveView):
- queryset = Article.objects.all()
- date_field = "pub_date"
- allow_future = True
Example myapp/urls.py:
- from django.urls import path
- from myapp.views import ArticleMonthArchiveView
- urlpatterns = [
- # Example: /2012/08/
- 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"),
- ]
Example myapp/article_archive_month.html:
- <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>
WeekArchiveView
- class
WeekArchiveView
[源代码] - 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)
django.views.generic.list.MultipleObjectTemplateResponseMixin
django.views.generic.base.TemplateResponseMixin
django.views.generic.dates.BaseWeekArchiveView
django.views.generic.dates.YearMixin
django.views.generic.dates.WeekMixin
django.views.generic.dates.BaseDateListView
django.views.generic.list.MultipleObjectMixin
django.views.generic.dates.DateMixin
django.views.generic.base.View
Context
In addition to the context provided byMultipleObjectMixin
(viaBaseDateListView
), the template'scontext will be:
week
: Adate
objectrepresenting the first day of the given week.next_week
: Adate
objectrepresenting the first day of the next week, according toallow_empty
andallow_future
.previous_week
: Adate
objectrepresenting the first day of the previous week, according toallow_empty
andallow_future
.
NotesUses a default
template_name_suffix
of_archive_week
.- The
week_format
attribute is astrptime()
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:
- from django.views.generic.dates import WeekArchiveView
- from myapp.models import Article
- class ArticleWeekArchiveView(WeekArchiveView):
- queryset = Article.objects.all()
- date_field = "pub_date"
- week_format = "%W"
- allow_future = True
Example myapp/urls.py:
- from django.urls import path
- from myapp.views import ArticleWeekArchiveView
- urlpatterns = [
- # Example: /2012/week/23/
- path('<int:year>/week/<int:week>/',
- ArticleWeekArchiveView.as_view(),
- name="archive_week"),
- ]
Example myapp/article_archive_week.html:
- <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 %}
- Previous Week: {{ previous_week|date:"W" }} of year {{ previous_week|date:"Y" }}
- {% endif %}
- {% if previous_week and next_week %}--{% endif %}
- {% if next_week %}
- Next week: {{ next_week|date:"W" }} of year {{ next_week|date:"Y" }}
- {% endif %}
- </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 date
are 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
[源代码] - 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
toTrue
.
Ancestors (MRO)
django.views.generic.list.MultipleObjectTemplateResponseMixin
django.views.generic.base.TemplateResponseMixin
django.views.generic.dates.BaseDayArchiveView
django.views.generic.dates.YearMixin
django.views.generic.dates.MonthMixin
django.views.generic.dates.DayMixin
django.views.generic.dates.BaseDateListView
django.views.generic.list.MultipleObjectMixin
django.views.generic.dates.DateMixin
django.views.generic.base.View
Context
In addition to the context provided byMultipleObjectMixin
(viaBaseDateListView
), the template'scontext will be:
day
: Adate
objectrepresenting the given day.next_day
: Adate
objectrepresenting the next day, according toallow_empty
andallow_future
.previous_day
: Adate
objectrepresenting the previous day, according toallow_empty
andallow_future
.next_month
: Adate
objectrepresenting the first day of the next month, according toallow_empty
andallow_future
.previous_month
: Adate
objectrepresenting the first day of the previous month, according toallow_empty
andallow_future
.
NotesUses a default
template_name_suffix
of_archive_day
.
Example myapp/views.py:
- from django.views.generic.dates import DayArchiveView
- from myapp.models import Article
- class ArticleDayArchiveView(DayArchiveView):
- queryset = Article.objects.all()
- date_field = "pub_date"
- allow_future = True
Example myapp/urls.py:
- from django.urls import path
- from myapp.views import ArticleDayArchiveView
- urlpatterns = [
- # Example: /2012/nov/10/
- path('<int:year>/<str:month>/<int:day>/',
- ArticleDayArchiveView.as_view(),
- name="archive_day"),
- ]
Example myapp/article_archive_day.html:
- <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>
TodayArchiveView
- class
TodayArchiveView
[源代码] - A day archive page showing all objects for today. This is exactly thesame as
django.views.generic.dates.DayArchiveView
, except today'sdate is used instead of theyear
/month
/day
arguments.
Ancestors (MRO)
django.views.generic.list.MultipleObjectTemplateResponseMixin
django.views.generic.base.TemplateResponseMixin
django.views.generic.dates.BaseTodayArchiveView
django.views.generic.dates.BaseDayArchiveView
django.views.generic.dates.YearMixin
django.views.generic.dates.MonthMixin
django.views.generic.dates.DayMixin
django.views.generic.dates.BaseDateListView
django.views.generic.list.MultipleObjectMixin
django.views.generic.dates.DateMixin
Uses a default
template_name_suffix
of_archive_today
.
Example myapp/views.py:
- from django.views.generic.dates import TodayArchiveView
- from myapp.models import Article
- class ArticleTodayArchiveView(TodayArchiveView):
- queryset = Article.objects.all()
- date_field = "pub_date"
- allow_future = True
Example myapp/urls.py:
- from django.urls import path
- from myapp.views import ArticleTodayArchiveView
- urlpatterns = [
- path('today/',
- ArticleTodayArchiveView.as_view(),
- name="archive_today"),
- ]
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
[源代码] - 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 set
allow_future
toTrue
.
Ancestors (MRO)
django.views.generic.detail.SingleObjectTemplateResponseMixin
django.views.generic.base.TemplateResponseMixin
django.views.generic.dates.BaseDateDetailView
django.views.generic.dates.YearMixin
django.views.generic.dates.MonthMixin
django.views.generic.dates.DayMixin
django.views.generic.dates.DateMixin
django.views.generic.detail.BaseDetailView
django.views.generic.detail.SingleObjectMixin
Includes the single object associated with the
model
specified intheDateDetailView
.
NotesUses a default
template_name_suffix
of_detail
.
Example myapp/urls.py:
- from django.urls import path
- from django.views.generic.dates import DateDetailView
- urlpatterns = [
- path('<int:year>/<str:month>/<int:day>/<int:pk>/',
- DateDetailView.as_view(model=Article, date_field="pub_date"),
- name="archive_date_detail"),
- ]
Example myapp/article_detail.html:
- <h1>{{ object.title }}</h1>
注解
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
):