基于日期的通用视图,在中提供 django.views.generic.dates ,是用于显示基于日期的数据的明细页的视图。
备注
本页中的一些示例假定 Article 模型定义如下 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¶按日期显示“最新”对象的顶级索引页。对象中包含日期 未来 除非您设置,否则不包括在内 allow_future 到 True .
Ancestors (MRO)
Context
除了提供的上下文 django.views.generic.list.MultipleObjectMixin (通过 django.views.generic.dates.BaseDateListView )模板的上下文为:
date_list 答: QuerySet 对象,其中包含根据 queryset ,表示为 datetime.datetime 对象,按降序排列。
Notes
使用默认值 context_object_name 属于 latest .
使用默认值 template_name_suffix 属于 _archive .
默认为提供 date_list 按年,但可以使用属性将其更改为月或日 date_list_period . 这也适用于所有子类视图。
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>
这将输出所有文章。
YearArchiveView¶显示给定年份中所有可用月份的年度存档页。对象中包含日期 未来 除非设置,否则不显示 allow_future 到 True .
Ancestors (MRO)
一个布尔值,指定是否检索今年的完整对象列表并将其传递给模板。如果 True ,对象列表将对上下文可用。如果 False , the None 查询集将用作对象列表。默认情况下,这是 False .
确定对象列表是否将作为上下文的一部分返回。退换商品 make_object_list 默认情况下。
Context
除了提供的上下文 django.views.generic.list.MultipleObjectMixin (通过 django.views.generic.dates.BaseDateListView )模板的上下文为:
date_list 答: QuerySet 对象,其中包含根据 queryset ,表示为 datetime.datetime 对象,按升序排列。
year 答: date 表示给定年份的对象。
next_year 答: date 根据 allow_empty 和 allow_future .
previous_year 答: date 表示上一年的第一天的对象,根据 allow_empty 和 allow_future .
Notes
使用默认值 template_name_suffix 属于 _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¶显示给定月份中所有对象的每月存档页面。对象中包含日期 未来 除非设置,否则不显示 allow_future 到 True .
Ancestors (MRO)
Context
除了提供的上下文 MultipleObjectMixin (通过 BaseDateListView )模板的上下文为:
date_list 答: QuerySet 根据 queryset ,表示为 datetime.datetime 对象,按升序排列。
month 答: date 表示给定月份的对象。
next_month 答: date 表示下个月第一天的对象,根据 allow_empty 和 allow_future .
previous_month 答: date 表示上个月的第一天的对象,根据 allow_empty 和 allow_future .
Notes
使用默认值 template_name_suffix 属于 _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¶显示给定周内所有对象的每周存档页。对象中包含日期 未来 除非设置,否则不显示 allow_future 到 True .
Ancestors (MRO)
Context
除了提供的上下文 MultipleObjectMixin (通过 BaseDateListView )模板的上下文为:
week 答: date 表示给定周的第一天的对象。
next_week 答: date 根据 allow_empty 和 allow_future .
previous_week 答: date 表示上周第一天的对象,根据 allow_empty 和 allow_future .
Notes
使用默认值 template_name_suffix 属于 _archive_week .
这个 week_format 属性是 strptime() 用于分析周数的格式字符串。支持以下值:
'%U' :基于星期日开始的美国周制。这是默认值。
'%W' 类似于 '%U' ,但假设一周从星期一开始。这与ISO 8601周数不同。
'%V' :ISO 8601周号,其中一周从周一开始。
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>
在本例中,您将输出周数。记住,周数由 date 模板过滤器 'W' 格式字符与 strftime() 和 strptime() 与 '%W' 格式化字符串。例如,对于2015年,周数输出 date 比输出高一倍 strftime() . 没有对应的 '%U' strftime() 字符串格式 date . 因此,应避免使用 date 为生成URL WeekArchiveView .
DayArchiveView¶显示给定日期中所有对象的日存档页。在未来的几天中抛出404错误,不管未来几天是否存在任何对象,除非您设置 allow_future 到 True .
Ancestors (MRO)
Context
除了提供的上下文 MultipleObjectMixin (通过 BaseDateListView )模板的上下文为:
day 答: date 表示给定日期的对象。
next_day 答: date 表示第二天的对象,根据 allow_empty 和 allow_future .
previous_day 答: date 表示前一天的对象,根据 allow_empty 和 allow_future .
next_month 答: date 表示下个月第一天的对象,根据 allow_empty 和 allow_future .
previous_month 答: date 表示上个月的第一天的对象,根据 allow_empty 和 allow_future .
Notes
使用默认值 template_name_suffix 属于 _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¶显示所有对象的一天存档页面 今天 . 这和 django.views.generic.dates.DayArchiveView ,除了使用今天的日期而不是 year/month /`` day``参数。
Ancestors (MRO)
Notes
使用默认值 template_name_suffix 属于 _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"),
]
示例模板在哪里 TodayArchiveView 是吗?
默认情况下,此视图使用与 DayArchiveView ,在前面的示例中。如果需要其他模板,请设置 template_name 属性作为新模板的名称。
DateDetailView¶表示单个对象的页。如果对象在将来有一个日期值,视图将默认抛出404错误,除非您设置 allow_future 到 True .
Ancestors (MRO)
Context
包括与 model 在中指定 DateDetailView .
Notes
使用默认值 template_name_suffix 属于 _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>
备注
上面列出的所有通用视图都有匹配的 Base 不同的观点不包括 MultipleObjectTemplateResponseMixin (用于存档视图)或 SingleObjectTemplateResponseMixin (对于 DateDetailView ):
5月 28, 2025