“Django/docs/3.1.x/ref/class-based-views/generic-date-based”的版本间差异

来自菜鸟教程
Django/docs/3.1.x/ref/class-based-views/generic-date-based
跳转至:导航、​搜索
(autoload)
 
(Page commit)
 
第1行: 第1行:
 +
{{DISPLAYTITLE:通用日期视图 — Django 文档}}
 
<div id="module-django.views.generic.dates" class="section">
 
<div id="module-django.views.generic.dates" class="section">
  
第4行: 第5行:
 
= 通用日期视图 =
 
= 通用日期视图 =
  
Django.views.generic.date中提供的基于日期的泛型视图是用于显示基于日期的数据的钻取页面的视图。
+
[[#module-django.views.generic.dates|django.views.generic.dates]] 中提供的基于日期的通用视图是用于显示基于日期数据的钻取页面的视图。
  
 
<div class="admonition note">
 
<div class="admonition note">
  
注解
+
笔记
  
Some of the examples on this page assume that an <code>Article</code> model has been
+
此页面上的一些示例假设 <code>Article</code> 模型已在 <code>myapp/models.py</code> 中定义如下:
defined as follows in <code>myapp/models.py</code>:
 
  
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
第17行: 第17行:
 
<div class="highlight">
 
<div class="highlight">
  
<pre>from django.db import models
+
<syntaxhighlight lang="python">from django.db import models
 
from django.urls import reverse
 
from django.urls import reverse
  
第25行: 第25行:
  
 
     def get_absolute_url(self):
 
     def get_absolute_url(self):
         return reverse('article-detail', kwargs={'pk': self.pk})</pre>
+
         return reverse('article-detail', kwargs={'pk': self.pk})</syntaxhighlight>
  
 
</div>
 
</div>
第34行: 第34行:
 
<div id="archiveindexview" class="section">
 
<div id="archiveindexview" class="section">
  
== <code>ArchiveIndexView</code> ==
+
== ArchiveIndexView ==
  
 
<dl>
 
<dl>
<dt>''class'' <code>ArchiveIndexView</code></dt>
+
<dt>''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">ArchiveIndexView</span></span></dt>
<dd><p>A top-level index page showing the &quot;latest&quot; objects, by date. Objects with
+
<dd><p>按日期显示“最新”对象的顶级索引页面。 除非您将 <code>allow_future</code> 设置为 <code>True</code>,否则不包括日期在 ''future'' 中的对象。</p>
a date in the ''future'' are not included unless you set <code>allow_future</code> to
+
<p>'''祖先 (MRO)'''</p>
<code>True</code>.</p>
 
<p>'''Ancestors (MRO)'''</p>
 
 
<ul>
 
<ul>
 
<li><p>[[../mixins-multiple-object#django.views.generic.list|<code>django.views.generic.list.MultipleObjectTemplateResponseMixin</code>]]</p></li>
 
<li><p>[[../mixins-multiple-object#django.views.generic.list|<code>django.views.generic.list.MultipleObjectTemplateResponseMixin</code>]]</p></li>
第51行: 第49行:
 
<li><p>[[../base#django.views.generic.base|<code>django.views.generic.base.View</code>]]</p></li></ul>
 
<li><p>[[../base#django.views.generic.base|<code>django.views.generic.base.View</code>]]</p></li></ul>
  
<p>'''Context'''</p>
+
<p>'''语境'''</p>
<p>In addition to the context provided by
+
<p>除了 [[../mixins-multiple-object#django.views.generic.list|django.views.generic.list.MultipleObjectMixin]](通过 [[../mixins-date-based#django.views.generic.dates|django.views.generic.dates.BaseDateListView]])提供的上下文,模板的上下文将是:</p>
[[../mixins-multiple-object#django.views.generic.list|<code>django.views.generic.list.MultipleObjectMixin</code>]] (via
 
[[../mixins-date-based#django.views.generic.dates|<code>django.views.generic.dates.BaseDateListView</code>]]), the template's
 
context will be:</p>
 
 
<ul>
 
<ul>
<li><p><code>date_list</code>: A [[../../models/querysets#django.db.models.query.QuerySet|<code>QuerySet</code>]]
+
<li><p><code>date_list</code>:一个 [[../../models/querysets#django.db.models.query.QuerySet|QuerySet]] 对象,包含根据 <code>queryset</code> 具有可用对象的所有年份,以降序表示为 <code>datetime.datetime</code> 对象。</p></li></ul>
object containing all years that have objects available according to
 
<code>queryset</code>, represented as <code>datetime.datetime</code> objects, in
 
descending order.</p></li></ul>
 
  
<p>'''Notes'''</p>
+
<p>'''笔记'''</p>
 
<ul>
 
<ul>
<li><p>Uses a default <code>context_object_name</code> of <code>latest</code>.</p></li>
+
<li><p>使用 <code>latest</code> 的默认 <code>context_object_name</code></p></li>
<li><p>Uses a default <code>template_name_suffix</code> of <code>_archive</code>.</p></li>
+
<li><p>使用 <code>_archive</code> 的默认 <code>template_name_suffix</code></p></li>
<li><p>Defaults to providing <code>date_list</code> by year, but this can be altered to
+
<li><p>默认为按年提供 <code>date_list</code>,但可以使用属性 <code>date_list_period</code> 将其更改为月或日。 这也适用于所有子类视图。</p></li></ul>
month or day using the attribute <code>date_list_period</code>. This also applies
 
to all subclass views.</p></li></ul>
 
  
<p>'''Example myapp/urls.py''':</p>
+
<p>'''示例 myapp/urls.py'''</p>
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>from django.urls import path
+
<syntaxhighlight lang="python">from django.urls import path
 
from django.views.generic.dates import ArchiveIndexView
 
from django.views.generic.dates import ArchiveIndexView
  
第82行: 第72行:
 
urlpatterns = [
 
urlpatterns = [
 
     path('archive/',
 
     path('archive/',
         ArchiveIndexView.as_view(model=Article, date_field=&quot;pub_date&quot;),
+
         ArchiveIndexView.as_view(model=Article, date_field="pub_date"),
         name=&quot;article_archive&quot;),
+
         name="article_archive"),
]</pre>
+
]</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<p>'''Example myapp/article_archive.html''':</p>
+
<p>'''示例 myapp/article_archive.html'''</p>
 
<div class="highlight-html+django notranslate">
 
<div class="highlight-html+django notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&lt;ul&gt;
+
<syntaxhighlight lang="html+django"><ul>
 
     {% for article in latest %}
 
     {% for article in latest %}
         &lt;li&gt;{{ article.pub_date }}: {{ article.title }}&lt;/li&gt;
+
         <li>{{ article.pub_date }}: {{ article.title }}</li>
 
     {% endfor %}
 
     {% endfor %}
&lt;/ul&gt;</pre>
+
</ul></syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<p>This will output all articles.</p></dd></dl>
+
<p>这将输出所有文章。</p></dd></dl>
  
  
第109行: 第99行:
 
<div id="yeararchiveview" class="section">
 
<div id="yeararchiveview" class="section">
  
== <code>YearArchiveView</code> ==
+
== YearArchiveView ==
  
 
<dl>
 
<dl>
<dt>''class'' <code>YearArchiveView</code></dt>
+
<dt>''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">YearArchiveView</span></span></dt>
<dd><p>A yearly archive page showing all available months in a given year. Objects
+
<dd><p>显示给定年份中所有可用月份的年度存档页面。 除非将 <code>allow_future</code> 设置为 <code>True</code>,否则不会显示日期在 ''future'' 中的对象。</p>
with a date in the ''future'' are not displayed unless you set
+
<p>'''祖先 (MRO)'''</p>
<code>allow_future</code> to <code>True</code>.</p>
 
<p>'''Ancestors (MRO)'''</p>
 
 
<ul>
 
<ul>
 
<li><p>[[../mixins-multiple-object#django.views.generic.list|<code>django.views.generic.list.MultipleObjectTemplateResponseMixin</code>]]</p></li>
 
<li><p>[[../mixins-multiple-object#django.views.generic.list|<code>django.views.generic.list.MultipleObjectTemplateResponseMixin</code>]]</p></li>
第128行: 第116行:
  
 
<dl>
 
<dl>
<dt><code>make_object_list</code></dt>
+
<dt><span class="sig-name descname"><span class="pre">make_object_list</span></span></dt>
<dd><p>A boolean specifying whether to retrieve the full list of objects for
+
<dd><p>一个布尔值,指定是否检索今年的完整对象列表并将它们传递给模板。 如果 <code>True</code>,对象列表将可用于上下文。 如果是 <code>False</code>,则 <code>None</code> 查询集将用作对象列表。 默认情况下,这是 <code>False</code></p></dd></dl>
this year and pass those to the template. If <code>True</code>, the list of
 
objects will be made available to the context. If <code>False</code>, the
 
<code>None</code> queryset will be used as the object list. By default, this is
 
<code>False</code>.</p></dd></dl>
 
  
 
<dl>
 
<dl>
<dt><code>get_make_object_list</code><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
+
<dt><span class="sig-name descname"><span class="pre">get_make_object_list</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span></dt>
<dd><p>Determine if an object list will be returned as part of the context.
+
<dd><p>确定对象列表是否将作为上下文的一部分返回。 默认返回 [[#django.views.generic.dates.YearArchiveView.make_object_list|make_object_list]]</p></dd></dl>
Returns [[#django.views.generic.dates.YearArchiveView.make_object_list|<code>make_object_list</code>]] by default.</p></dd></dl>
 
  
<p>'''Context'''</p>
+
<p>'''语境'''</p>
<p>In addition to the context provided by
+
<p>除了 [[../mixins-multiple-object#django.views.generic.list|django.views.generic.list.MultipleObjectMixin]](通过 [[../mixins-date-based#django.views.generic.dates|django.views.generic.dates.BaseDateListView]])提供的上下文,模板的上下文将是:</p>
[[../mixins-multiple-object#django.views.generic.list|<code>django.views.generic.list.MultipleObjectMixin</code>]] (via
 
[[../mixins-date-based#django.views.generic.dates|<code>django.views.generic.dates.BaseDateListView</code>]]), the template's
 
context will be:</p>
 
 
<ul>
 
<ul>
<li><p><code>date_list</code>: A [[../../models/querysets#django.db.models.query.QuerySet|<code>QuerySet</code>]]
+
<li><p><code>date_list</code>:一个 [[../../models/querysets#django.db.models.query.QuerySet|QuerySet]] 对象,包含根据 <code>queryset</code> 具有可用对象的所有月份,表示为 <code>datetime.datetime</code> 对象,按升序排列。</p></li>
object containing all months that have objects available according to
+
<li><p><code>year</code>:代表给定年份的 <code>date</code> 对象。</p></li>
<code>queryset</code>, represented as <code>datetime.datetime</code> objects, in
+
<li><p><code>next_year</code>:根据 [[../mixins-date-based#django.views.generic.dates.BaseDateListView|allow_empty]] [[../mixins-date-based#django.views.generic.dates.DateMixin|allow_future]],代表下一年第一天的 <code>date</code> 对象。</p></li>
ascending order.</p></li>
+
<li><p><code>previous_year</code>:根据 [[../mixins-date-based#django.views.generic.dates.BaseDateListView|allow_empty]] [[../mixins-date-based#django.views.generic.dates.DateMixin|allow_future]],表示前一年第一天的 <code>date</code> 对象。</p></li></ul>
<li><p><code>year</code>: A <code>date</code> object
 
representing the given year.</p></li>
 
<li><p><code>next_year</code>: A <code>date</code> object
 
representing the first day of the next year, according to
 
[[../mixins-date-based#django.views.generic.dates.BaseDateListView|<code>allow_empty</code>]] and
 
[[../mixins-date-based#django.views.generic.dates.DateMixin|<code>allow_future</code>]].</p></li>
 
<li><p><code>previous_year</code>: A <code>date</code> object
 
representing the first day of the previous year, according to
 
[[../mixins-date-based#django.views.generic.dates.BaseDateListView|<code>allow_empty</code>]] and
 
[[../mixins-date-based#django.views.generic.dates.DateMixin|<code>allow_future</code>]].</p></li></ul>
 
  
<p>'''Notes'''</p>
+
<p>'''笔记'''</p>
 
<ul>
 
<ul>
<li><p>Uses a default <code>template_name_suffix</code> of <code>_archive_year</code>.</p></li></ul>
+
<li><p>使用 <code>_archive_year</code> 的默认 <code>template_name_suffix</code></p></li></ul>
  
<p>'''Example myapp/views.py''':</p>
+
<p>'''示例 myapp/views.py'''</p>
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>from django.views.generic.dates import YearArchiveView
+
<syntaxhighlight lang="python">from django.views.generic.dates import YearArchiveView
  
 
from myapp.models import Article
 
from myapp.models import Article
第176行: 第146行:
 
class ArticleYearArchiveView(YearArchiveView):
 
class ArticleYearArchiveView(YearArchiveView):
 
     queryset = Article.objects.all()
 
     queryset = Article.objects.all()
     date_field = &quot;pub_date&quot;
+
     date_field = "pub_date"
 
     make_object_list = True
 
     make_object_list = True
     allow_future = True</pre>
+
     allow_future = True</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<p>'''Example myapp/urls.py''':</p>
+
<p>'''示例 myapp/urls.py'''</p>
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>from django.urls import path
+
<syntaxhighlight lang="python">from django.urls import path
  
 
from myapp.views import ArticleYearArchiveView
 
from myapp.views import ArticleYearArchiveView
  
 
urlpatterns = [
 
urlpatterns = [
     path('&lt;int:year&gt;/',
+
     path('<int:year>/',
 
         ArticleYearArchiveView.as_view(),
 
         ArticleYearArchiveView.as_view(),
         name=&quot;article_year_archive&quot;),
+
         name="article_year_archive"),
]</pre>
+
]</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<p>'''Example myapp/article_archive_year.html''':</p>
+
<p>'''示例 myapp/article_archive_year.html'''</p>
 
<div class="highlight-html+django notranslate">
 
<div class="highlight-html+django notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&lt;ul&gt;
+
<syntaxhighlight lang="html+django"><ul>
 
     {% for date in date_list %}
 
     {% for date in date_list %}
         &lt;li&gt;{{ date|date }}&lt;/li&gt;
+
         <li>{{ date|date }}</li>
 
     {% endfor %}
 
     {% endfor %}
&lt;/ul&gt;
+
</ul>
  
&lt;div&gt;
+
<div>
     &lt;h1&gt;All Articles for {{ year|date:&quot;Y&quot; }}&lt;/h1&gt;
+
     <h1>All Articles for {{ year|date:"Y" }}</h1>
 
     {% for obj in object_list %}
 
     {% for obj in object_list %}
         &lt;p&gt;
+
         <p>
             {{ obj.title }} - {{ obj.pub_date|date:&quot;F j, Y&quot; }}
+
             {{ obj.title }} - {{ obj.pub_date|date:"F j, Y" }}
         &lt;/p&gt;
+
         </p>
 
     {% endfor %}
 
     {% endfor %}
&lt;/div&gt;</pre>
+
</div></syntaxhighlight>
  
 
</div>
 
</div>
第229行: 第199行:
 
<div id="montharchiveview" class="section">
 
<div id="montharchiveview" class="section">
  
== <code>MonthArchiveView</code> ==
+
== MonthArchiveView ==
  
 
<dl>
 
<dl>
<dt>''class'' <code>MonthArchiveView</code></dt>
+
<dt>''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">MonthArchiveView</span></span></dt>
<dd><p>A monthly archive page showing all objects in a given month. Objects with a
+
<dd><p>显示给定月份中所有对象的每月存档页面。 除非将 <code>allow_future</code> 设置为 <code>True</code>,否则不会显示日期在 ''future'' 中的对象。</p>
date in the ''future'' are not displayed unless you set <code>allow_future</code> to
+
<p>'''祖先 (MRO)'''</p>
<code>True</code>.</p>
 
<p>'''Ancestors (MRO)'''</p>
 
 
<ul>
 
<ul>
 
<li><p>[[../mixins-multiple-object#django.views.generic.list|<code>django.views.generic.list.MultipleObjectTemplateResponseMixin</code>]]</p></li>
 
<li><p>[[../mixins-multiple-object#django.views.generic.list|<code>django.views.generic.list.MultipleObjectTemplateResponseMixin</code>]]</p></li>
第248行: 第216行:
 
<li><p>[[../base#django.views.generic.base|<code>django.views.generic.base.View</code>]]</p></li></ul>
 
<li><p>[[../base#django.views.generic.base|<code>django.views.generic.base.View</code>]]</p></li></ul>
  
<p>'''Context'''</p>
+
<p>'''语境'''</p>
<p>In addition to the context provided by
+
<p>除了 [[../mixins-multiple-object#django.views.generic.list|MultipleObjectMixin]](通过 [[../mixins-date-based#django.views.generic.dates|BaseDateListView]])提供的上下文之外,模板的上下文将是:</p>
[[../mixins-multiple-object#django.views.generic.list|<code>MultipleObjectMixin</code>]] (via
 
[[../mixins-date-based#django.views.generic.dates|<code>BaseDateListView</code>]]), the template's
 
context will be:</p>
 
 
<ul>
 
<ul>
<li><p><code>date_list</code>: A [[../../models/querysets#django.db.models.query.QuerySet|<code>QuerySet</code>]]
+
<li><p><code>date_list</code>:一个 [[../../models/querysets#django.db.models.query.QuerySet|QuerySet]] 对象,包含给定月份中所有有对象可用的天,根据 <code>queryset</code>,表示为 <code>datetime.datetime</code> 对象,按升序排列.</p></li>
object containing all days that have objects available in the given month,
+
<li><p><code>month</code>:代表给定月份的 <code>date</code> 对象。</p></li>
according to <code>queryset</code>, represented as <code>datetime.datetime</code>
+
<li><p><code>next_month</code>:根据 [[../mixins-date-based#django.views.generic.dates.BaseDateListView|allow_empty]] [[../mixins-date-based#django.views.generic.dates.DateMixin|allow_future]],表示下个月第一天的 <code>date</code> 对象。</p></li>
objects, in ascending order.</p></li>
+
<li><p><code>previous_month</code>:根据 [[../mixins-date-based#django.views.generic.dates.BaseDateListView|allow_empty]] [[../mixins-date-based#django.views.generic.dates.DateMixin|allow_future]],表示上个月第一天的 <code>date</code> 对象。</p></li></ul>
<li><p><code>month</code>: A <code>date</code> object
 
representing the given month.</p></li>
 
<li><p><code>next_month</code>: A <code>date</code> object
 
representing the first day of the next month, according to
 
[[../mixins-date-based#django.views.generic.dates.BaseDateListView|<code>allow_empty</code>]] and
 
[[../mixins-date-based#django.views.generic.dates.DateMixin|<code>allow_future</code>]].</p></li>
 
<li><p><code>previous_month</code>: A <code>date</code> object
 
representing the first day of the previous month, according to
 
[[../mixins-date-based#django.views.generic.dates.BaseDateListView|<code>allow_empty</code>]] and
 
[[../mixins-date-based#django.views.generic.dates.DateMixin|<code>allow_future</code>]].</p></li></ul>
 
  
<p>'''Notes'''</p>
+
<p>'''笔记'''</p>
 
<ul>
 
<ul>
<li><p>Uses a default <code>template_name_suffix</code> of <code>_archive_month</code>.</p></li></ul>
+
<li><p>使用 <code>_archive_month</code> 的默认 <code>template_name_suffix</code></p></li></ul>
  
<p>'''Example myapp/views.py''':</p>
+
<p>'''示例 myapp/views.py'''</p>
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>from django.views.generic.dates import MonthArchiveView
+
<syntaxhighlight lang="python">from django.views.generic.dates import MonthArchiveView
  
 
from myapp.models import Article
 
from myapp.models import Article
第284行: 第239行:
 
class ArticleMonthArchiveView(MonthArchiveView):
 
class ArticleMonthArchiveView(MonthArchiveView):
 
     queryset = Article.objects.all()
 
     queryset = Article.objects.all()
     date_field = &quot;pub_date&quot;
+
     date_field = "pub_date"
     allow_future = True</pre>
+
     allow_future = True</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<p>'''Example myapp/urls.py''':</p>
+
<p>'''示例 myapp/urls.py'''</p>
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>from django.urls import path
+
<syntaxhighlight lang="python">from django.urls import path
  
 
from myapp.views import ArticleMonthArchiveView
 
from myapp.views import ArticleMonthArchiveView
第301行: 第256行:
 
urlpatterns = [
 
urlpatterns = [
 
     # Example: /2012/08/
 
     # Example: /2012/08/
     path('&lt;int:year&gt;/&lt;int:month&gt;/',
+
     path('<int:year>/<int:month>/',
 
         ArticleMonthArchiveView.as_view(month_format='%m'),
 
         ArticleMonthArchiveView.as_view(month_format='%m'),
         name=&quot;archive_month_numeric&quot;),
+
         name="archive_month_numeric"),
 
     # Example: /2012/aug/
 
     # Example: /2012/aug/
     path('&lt;int:year&gt;/&lt;str:month&gt;/',
+
     path('<int:year>/<str:month>/',
 
         ArticleMonthArchiveView.as_view(),
 
         ArticleMonthArchiveView.as_view(),
         name=&quot;archive_month&quot;),
+
         name="archive_month"),
]</pre>
+
]</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<p>'''Example myapp/article_archive_month.html''':</p>
+
<p>'''示例 myapp/article_archive_month.html'''</p>
 
<div class="highlight-html+django notranslate">
 
<div class="highlight-html+django notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&lt;ul&gt;
+
<syntaxhighlight lang="html+django"><ul>
 
     {% for article in object_list %}
 
     {% for article in object_list %}
         &lt;li&gt;{{ article.pub_date|date:&quot;F j, Y&quot; }}: {{ article.title }}&lt;/li&gt;
+
         <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
 
     {% endfor %}
 
     {% endfor %}
&lt;/ul&gt;
+
</ul>
  
&lt;p&gt;
+
<p>
 
     {% if previous_month %}
 
     {% if previous_month %}
         Previous Month: {{ previous_month|date:&quot;F Y&quot; }}
+
         Previous Month: {{ previous_month|date:"F Y" }}
 
     {% endif %}
 
     {% endif %}
 
     {% if next_month %}
 
     {% if next_month %}
         Next Month: {{ next_month|date:&quot;F Y&quot; }}
+
         Next Month: {{ next_month|date:"F Y" }}
 
     {% endif %}
 
     {% endif %}
&lt;/p&gt;</pre>
+
</p></syntaxhighlight>
  
 
</div>
 
</div>
第341行: 第296行:
 
<div id="weekarchiveview" class="section">
 
<div id="weekarchiveview" class="section">
  
== <code>WeekArchiveView</code> ==
+
== WeekArchiveView ==
  
 
<dl>
 
<dl>
<dt>''class'' <code>WeekArchiveView</code></dt>
+
<dt>''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">WeekArchiveView</span></span></dt>
<dd><p>A weekly archive page showing all objects in a given week. Objects with a
+
<dd><p>显示给定周内所有对象的每周存档页面。 除非将 <code>allow_future</code> 设置为 <code>True</code>,否则不会显示日期在 ''future'' 中的对象。</p>
date in the ''future'' are not displayed unless you set <code>allow_future</code> to
+
<p>'''祖先 (MRO)'''</p>
<code>True</code>.</p>
 
<p>'''Ancestors (MRO)'''</p>
 
 
<ul>
 
<ul>
 
<li><p>[[../mixins-multiple-object#django.views.generic.list|<code>django.views.generic.list.MultipleObjectTemplateResponseMixin</code>]]</p></li>
 
<li><p>[[../mixins-multiple-object#django.views.generic.list|<code>django.views.generic.list.MultipleObjectTemplateResponseMixin</code>]]</p></li>
第360行: 第313行:
 
<li><p>[[../base#django.views.generic.base|<code>django.views.generic.base.View</code>]]</p></li></ul>
 
<li><p>[[../base#django.views.generic.base|<code>django.views.generic.base.View</code>]]</p></li></ul>
  
<p>'''Context'''</p>
+
<p>'''语境'''</p>
<p>In addition to the context provided by
+
<p>除了 [[../mixins-multiple-object#django.views.generic.list|MultipleObjectMixin]](通过 [[../mixins-date-based#django.views.generic.dates|BaseDateListView]])提供的上下文之外,模板的上下文将是:</p>
[[../mixins-multiple-object#django.views.generic.list|<code>MultipleObjectMixin</code>]] (via
 
[[../mixins-date-based#django.views.generic.dates|<code>BaseDateListView</code>]]), the template's
 
context will be:</p>
 
 
<ul>
 
<ul>
<li><p><code>week</code>: A <code>date</code> object
+
<li><p><code>week</code>:表示给定周的第一天的 <code>date</code> 对象。</p></li>
representing the first day of the given week.</p></li>
+
<li><p><code>next_week</code>:根据 [[../mixins-date-based#django.views.generic.dates.BaseDateListView|allow_empty]] [[../mixins-date-based#django.views.generic.dates.DateMixin|allow_future]],表示下周第一天的 <code>date</code> 对象。</p></li>
<li><p><code>next_week</code>: A <code>date</code> object
+
<li><p><code>previous_week</code>:根据 [[../mixins-date-based#django.views.generic.dates.BaseDateListView|allow_empty]] [[../mixins-date-based#django.views.generic.dates.DateMixin|allow_future]],代表上周第一天的 <code>date</code> 对象。</p></li></ul>
representing the first day of the next week, according to
 
[[../mixins-date-based#django.views.generic.dates.BaseDateListView|<code>allow_empty</code>]] and
 
[[../mixins-date-based#django.views.generic.dates.DateMixin|<code>allow_future</code>]].</p></li>
 
<li><p><code>previous_week</code>: A <code>date</code> object
 
representing the first day of the previous week, according to
 
[[../mixins-date-based#django.views.generic.dates.BaseDateListView|<code>allow_empty</code>]] and
 
[[../mixins-date-based#django.views.generic.dates.DateMixin|<code>allow_future</code>]].</p></li></ul>
 
  
<p>'''Notes'''</p>
+
<p>'''笔记'''</p>
 
<ul>
 
<ul>
<li><p>Uses a default <code>template_name_suffix</code> of <code>_archive_week</code>.</p></li>
+
<li><p>使用 <code>_archive_week</code> 的默认 <code>template_name_suffix</code></p></li>
<li><p>The <code>week_format</code> attribute is a <code>strptime()</code> format string
+
<li><p><code>week_format</code> 属性是用于解析周数的 <code>strptime()</code> 格式字符串。 支持以下值:</p>
used to parse the week number. The following values are supported:</p>
 
 
<ul>
 
<ul>
<li><p><code>'%U'</code>: Based on the United States week system where the week
+
<li><p><code>'%U'</code>:基于美国的星期系统,星期从星期日开始。 这是默认值。</p></li>
begins on Sunday. This is the default value.</p></li>
+
<li><p><code>'%W'</code>:与 <code>'%U'</code> 类似,只是它假设一周从星期一开始。 这与 ISO 8601 周数不同。</p></li></ul>
<li><p><code>'%W'</code>: Similar to <code>'%U'</code>, except it assumes that the week
 
begins on Monday. This is not the same as the ISO 8601 week number.</p></li></ul>
 
 
</li></ul>
 
</li></ul>
  
<p>'''Example myapp/views.py''':</p>
+
<p>'''示例 myapp/views.py'''</p>
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>from django.views.generic.dates import WeekArchiveView
+
<syntaxhighlight lang="python">from django.views.generic.dates import WeekArchiveView
  
 
from myapp.models import Article
 
from myapp.models import Article
第400行: 第340行:
 
class ArticleWeekArchiveView(WeekArchiveView):
 
class ArticleWeekArchiveView(WeekArchiveView):
 
     queryset = Article.objects.all()
 
     queryset = Article.objects.all()
     date_field = &quot;pub_date&quot;
+
     date_field = "pub_date"
     week_format = &quot;%W&quot;
+
     week_format = "%W"
     allow_future = True</pre>
+
     allow_future = True</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<p>'''Example myapp/urls.py''':</p>
+
<p>'''示例 myapp/urls.py'''</p>
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>from django.urls import path
+
<syntaxhighlight lang="python">from django.urls import path
  
 
from myapp.views import ArticleWeekArchiveView
 
from myapp.views import ArticleWeekArchiveView
第418行: 第358行:
 
urlpatterns = [
 
urlpatterns = [
 
     # Example: /2012/week/23/
 
     # Example: /2012/week/23/
     path('&lt;int:year&gt;/week/&lt;int:week&gt;/',
+
     path('<int:year>/week/<int:week>/',
 
         ArticleWeekArchiveView.as_view(),
 
         ArticleWeekArchiveView.as_view(),
         name=&quot;archive_week&quot;),
+
         name="archive_week"),
]</pre>
+
]</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<p>'''Example myapp/article_archive_week.html''':</p>
+
<p>'''示例 myapp/article_archive_week.html'''</p>
 
<div class="highlight-html+django notranslate">
 
<div class="highlight-html+django notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&lt;h1&gt;Week {{ week|date:'W' }}&lt;/h1&gt;
+
<syntaxhighlight lang="html+django"><h1>Week {{ week|date:'W' }}</h1>
  
&lt;ul&gt;
+
<ul>
 
     {% for article in object_list %}
 
     {% for article in object_list %}
         &lt;li&gt;{{ article.pub_date|date:&quot;F j, Y&quot; }}: {{ article.title }}&lt;/li&gt;
+
         <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
 
     {% endfor %}
 
     {% endfor %}
&lt;/ul&gt;
+
</ul>
  
&lt;p&gt;
+
<p>
 
     {% if previous_week %}
 
     {% if previous_week %}
         Previous Week: {{ previous_week|date:&quot;W&quot; }} of year {{ previous_week|date:&quot;Y&quot; }}
+
         Previous Week: {{ previous_week|date:"W" }} of year {{ previous_week|date:"Y" }}
 
     {% endif %}
 
     {% endif %}
 
     {% if previous_week and next_week %}--{% endif %}
 
     {% if previous_week and next_week %}--{% endif %}
 
     {% if next_week %}
 
     {% if next_week %}
         Next week: {{ next_week|date:&quot;W&quot; }} of year {{ next_week|date:&quot;Y&quot; }}
+
         Next week: {{ next_week|date:"W" }} of year {{ next_week|date:"Y" }}
 
     {% endif %}
 
     {% endif %}
&lt;/p&gt;</pre>
+
</p></syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<p>In this example, you are outputting the week number. Keep in mind that week
+
<p>在本例中,您将输出周数。 请记住,使用 <code>'W'</code> 格式字符的 [[#id1|:tfilter:`date`]] 模板过滤器计算的周数并不总是与 <code>strftime()</code> 和 [ X183X] 与 <code>'%W'</code> 格式字符串。 例如,对于 2015 年,[[#id3|:tfilter:`date`]] 输出的周数比 <code>strftime()</code> 输出的周数高 1。 [[#id5|:tfilter:`date`]] 中的 <code>'%U'</code> <code>strftime()</code> 格式字符串没有等效项。 因此,您应该避免使用 [[#id7|:tfilter:`date`]] <code>WeekArchiveView</code> 生成 URL。</p></dd></dl>
numbers computed by the [[../../templates/builtins#std-templatefilter-date|<code>date</code>]] template filter with the <code>'W'</code>
 
format character are not always the same as those computed by
 
<code>strftime()</code> and <code>strptime()</code> with the <code>'%W'</code> format
 
string. For year 2015, for example, week numbers output by [[../../templates/builtins#std-templatefilter-date|<code>date</code>]]
 
are higher by one compared to those output by <code>strftime()</code>. There
 
isn't an equivalent for the <code>'%U'</code> <code>strftime()</code> format string
 
in [[../../templates/builtins#std-templatefilter-date|<code>date</code>]]. Therefore, you should avoid using [[../../templates/builtins#std-templatefilter-date|<code>date</code>]] to
 
generate URLs for <code>WeekArchiveView</code>.</p></dd></dl>
 
  
  
第466行: 第398行:
 
<div id="dayarchiveview" class="section">
 
<div id="dayarchiveview" class="section">
  
== <code>DayArchiveView</code> ==
+
== DayArchiveView ==
  
 
<dl>
 
<dl>
<dt>''class'' <code>DayArchiveView</code></dt>
+
<dt>''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">DayArchiveView</span></span></dt>
<dd><p>A day archive page showing all objects in a given day. Days in the future
+
<dd><p>一天存档页面,显示给定日期内的所有对象。 未来几天会抛出 404 错误,无论未来几天是否存在任何对象,除非您将 <code>allow_future</code> 设置为 <code>True</code></p>
throw a 404 error, regardless of whether any objects exist for future days,
+
<p>'''祖先 (MRO)'''</p>
unless you set <code>allow_future</code> to <code>True</code>.</p>
 
<p>'''Ancestors (MRO)'''</p>
 
 
<ul>
 
<ul>
 
<li><p>[[../mixins-multiple-object#django.views.generic.list|<code>django.views.generic.list.MultipleObjectTemplateResponseMixin</code>]]</p></li>
 
<li><p>[[../mixins-multiple-object#django.views.generic.list|<code>django.views.generic.list.MultipleObjectTemplateResponseMixin</code>]]</p></li>
第486行: 第416行:
 
<li><p>[[../base#django.views.generic.base|<code>django.views.generic.base.View</code>]]</p></li></ul>
 
<li><p>[[../base#django.views.generic.base|<code>django.views.generic.base.View</code>]]</p></li></ul>
  
<p>'''Context'''</p>
+
<p>'''语境'''</p>
<p>In addition to the context provided by
+
<p>除了 [[../mixins-multiple-object#django.views.generic.list|MultipleObjectMixin]](通过 [[../mixins-date-based#django.views.generic.dates|BaseDateListView]])提供的上下文之外,模板的上下文将是:</p>
[[../mixins-multiple-object#django.views.generic.list|<code>MultipleObjectMixin</code>]] (via
 
[[../mixins-date-based#django.views.generic.dates|<code>BaseDateListView</code>]]), the template's
 
context will be:</p>
 
 
<ul>
 
<ul>
<li><p><code>day</code>: A <code>date</code> object
+
<li><p><code>day</code>:代表给定日期的 <code>date</code> 对象。</p></li>
representing the given day.</p></li>
+
<li><p><code>next_day</code>:根据 [[../mixins-date-based#django.views.generic.dates.BaseDateListView|allow_empty]] [[../mixins-date-based#django.views.generic.dates.DateMixin|allow_future]],代表第二天的 <code>date</code> 对象。</p></li>
<li><p><code>next_day</code>: A <code>date</code> object
+
<li><p><code>previous_day</code>:代表前一天的 <code>date</code> 对象,根据 [[../mixins-date-based#django.views.generic.dates.BaseDateListView|allow_empty]] [[../mixins-date-based#django.views.generic.dates.DateMixin|allow_future]]</p></li>
representing the next day, according to
+
<li><p><code>next_month</code>:根据 [[../mixins-date-based#django.views.generic.dates.BaseDateListView|allow_empty]] [[../mixins-date-based#django.views.generic.dates.DateMixin|allow_future]],表示下个月第一天的 <code>date</code> 对象。</p></li>
[[../mixins-date-based#django.views.generic.dates.BaseDateListView|<code>allow_empty</code>]] and
+
<li><p><code>previous_month</code>:根据 [[../mixins-date-based#django.views.generic.dates.BaseDateListView|allow_empty]] [[../mixins-date-based#django.views.generic.dates.DateMixin|allow_future]],表示上个月第一天的 <code>date</code> 对象。</p></li></ul>
[[../mixins-date-based#django.views.generic.dates.DateMixin|<code>allow_future</code>]].</p></li>
 
<li><p><code>previous_day</code>: A <code>date</code> object
 
representing the previous day, according to
 
[[../mixins-date-based#django.views.generic.dates.BaseDateListView|<code>allow_empty</code>]] and
 
[[../mixins-date-based#django.views.generic.dates.DateMixin|<code>allow_future</code>]].</p></li>
 
<li><p><code>next_month</code>: A <code>date</code> object
 
representing the first day of the next month, according to
 
[[../mixins-date-based#django.views.generic.dates.BaseDateListView|<code>allow_empty</code>]] and
 
[[../mixins-date-based#django.views.generic.dates.DateMixin|<code>allow_future</code>]].</p></li>
 
<li><p><code>previous_month</code>: A <code>date</code> object
 
representing the first day of the previous month, according to
 
[[../mixins-date-based#django.views.generic.dates.BaseDateListView|<code>allow_empty</code>]] and
 
[[../mixins-date-based#django.views.generic.dates.DateMixin|<code>allow_future</code>]].</p></li></ul>
 
  
<p>'''Notes'''</p>
+
<p>'''笔记'''</p>
 
<ul>
 
<ul>
<li><p>Uses a default <code>template_name_suffix</code> of <code>_archive_day</code>.</p></li></ul>
+
<li><p>使用 <code>_archive_day</code> 的默认 <code>template_name_suffix</code></p></li></ul>
  
<p>'''Example myapp/views.py''':</p>
+
<p>'''示例 myapp/views.py'''</p>
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>from django.views.generic.dates import DayArchiveView
+
<syntaxhighlight lang="python">from django.views.generic.dates import DayArchiveView
  
 
from myapp.models import Article
 
from myapp.models import Article
第526行: 第440行:
 
class ArticleDayArchiveView(DayArchiveView):
 
class ArticleDayArchiveView(DayArchiveView):
 
     queryset = Article.objects.all()
 
     queryset = Article.objects.all()
     date_field = &quot;pub_date&quot;
+
     date_field = "pub_date"
     allow_future = True</pre>
+
     allow_future = True</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<p>'''Example myapp/urls.py''':</p>
+
<p>'''示例 myapp/urls.py'''</p>
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>from django.urls import path
+
<syntaxhighlight lang="python">from django.urls import path
  
 
from myapp.views import ArticleDayArchiveView
 
from myapp.views import ArticleDayArchiveView
第543行: 第457行:
 
urlpatterns = [
 
urlpatterns = [
 
     # Example: /2012/nov/10/
 
     # Example: /2012/nov/10/
     path('&lt;int:year&gt;/&lt;str:month&gt;/&lt;int:day&gt;/',
+
     path('<int:year>/<str:month>/<int:day>/',
 
         ArticleDayArchiveView.as_view(),
 
         ArticleDayArchiveView.as_view(),
         name=&quot;archive_day&quot;),
+
         name="archive_day"),
]</pre>
+
]</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<p>'''Example myapp/article_archive_day.html''':</p>
+
<p>'''示例 myapp/article_archive_day.html'''</p>
 
<div class="highlight-html+django notranslate">
 
<div class="highlight-html+django notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&lt;h1&gt;{{ day }}&lt;/h1&gt;
+
<syntaxhighlight lang="html+django"><h1>{{ day }}</h1>
  
&lt;ul&gt;
+
<ul>
 
     {% for article in object_list %}
 
     {% for article in object_list %}
         &lt;li&gt;{{ article.pub_date|date:&quot;F j, Y&quot; }}: {{ article.title }}&lt;/li&gt;
+
         <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
 
     {% endfor %}
 
     {% endfor %}
&lt;/ul&gt;
+
</ul>
  
&lt;p&gt;
+
<p>
 
     {% if previous_day %}
 
     {% if previous_day %}
 
         Previous Day: {{ previous_day }}
 
         Previous Day: {{ previous_day }}
第572行: 第486行:
 
         Next Day: {{ next_day }}
 
         Next Day: {{ next_day }}
 
     {% endif %}
 
     {% endif %}
&lt;/p&gt;</pre>
+
</p></syntaxhighlight>
  
 
</div>
 
</div>
第582行: 第496行:
 
<div id="todayarchiveview" class="section">
 
<div id="todayarchiveview" class="section">
  
== <code>TodayArchiveView</code> ==
+
== TodayArchiveView ==
  
 
<dl>
 
<dl>
<dt>''class'' <code>TodayArchiveView</code></dt>
+
<dt>''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">TodayArchiveView</span></span></dt>
<dd><p>A day archive page showing all objects for ''today''. This is exactly the
+
<dd><p>显示 ''today'' 的所有对象的日存档页面。 这与 [[#django.views.generic.dates.DayArchiveView|django.views.generic.dates.DayArchiveView]] 完全相同,除了使用今天的日期而不是 <code>year</code>/<code>month</code>/<code>day</code>论据。</p>
same as [[#django.views.generic.dates.DayArchiveView|<code>django.views.generic.dates.DayArchiveView</code>]], except today's
+
<p>'''祖先 (MRO)'''</p>
date is used instead of the <code>year</code>/<code>month</code>/<code>day</code> arguments.</p>
 
<p>'''Ancestors (MRO)'''</p>
 
 
<ul>
 
<ul>
 
<li><p>[[../mixins-multiple-object#django.views.generic.list|<code>django.views.generic.list.MultipleObjectTemplateResponseMixin</code>]]</p></li>
 
<li><p>[[../mixins-multiple-object#django.views.generic.list|<code>django.views.generic.list.MultipleObjectTemplateResponseMixin</code>]]</p></li>
第603行: 第515行:
 
<li><p>[[../base#django.views.generic.base|<code>django.views.generic.base.View</code>]]</p></li></ul>
 
<li><p>[[../base#django.views.generic.base|<code>django.views.generic.base.View</code>]]</p></li></ul>
  
<p>'''Notes'''</p>
+
<p>'''笔记'''</p>
 
<ul>
 
<ul>
<li><p>使用默认的 <code>template_name_suffix</code> of <code>_archive_today</code> 。</p></li></ul>
+
<li><p>使用 <code>_archive_today</code> 的默认 <code>template_name_suffix</code>。</p></li></ul>
  
<p>'''Example myapp/views.py''':</p>
+
<p>'''示例 myapp/views.py'''</p>
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>from django.views.generic.dates import TodayArchiveView
+
<syntaxhighlight lang="python">from django.views.generic.dates import TodayArchiveView
  
 
from myapp.models import Article
 
from myapp.models import Article
第618行: 第530行:
 
class ArticleTodayArchiveView(TodayArchiveView):
 
class ArticleTodayArchiveView(TodayArchiveView):
 
     queryset = Article.objects.all()
 
     queryset = Article.objects.all()
     date_field = &quot;pub_date&quot;
+
     date_field = "pub_date"
     allow_future = True</pre>
+
     allow_future = True</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<p>'''Example myapp/urls.py''':</p>
+
<p>'''示例 myapp/urls.py'''</p>
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>from django.urls import path
+
<syntaxhighlight lang="python">from django.urls import path
  
 
from myapp.views import ArticleTodayArchiveView
 
from myapp.views import ArticleTodayArchiveView
第636行: 第548行:
 
     path('today/',
 
     path('today/',
 
         ArticleTodayArchiveView.as_view(),
 
         ArticleTodayArchiveView.as_view(),
         name=&quot;archive_today&quot;),
+
         name="archive_today"),
]</pre>
+
]</syntaxhighlight>
  
 
</div>
 
</div>
第644行: 第556行:
 
<div class="admonition-where-is-the-example-template-for-todayarchiveview admonition">
 
<div class="admonition-where-is-the-example-template-for-todayarchiveview admonition">
  
<p>Where is the example template for <code>TodayArchiveView</code>?</p>
+
<p><code>TodayArchiveView</code> 的示例模板在哪里?</p>
<p>This view uses by default the same template as the
+
<p>默认情况下,此视图使用与 [[../flattened-index#DayArchiveView|DayArchiveView]] 相同的模板,这在前面的示例中。 如果您需要不同的模板,请将 <code>template_name</code> 属性设置为新模板的名称。</p>
[[../flattened-index#DayArchiveView|<code>DayArchiveView</code>]], which is in the previous example. If you need
 
a different template, set the <code>template_name</code> attribute to be the
 
name of the new template.</p>
 
  
 
</div></dd></dl>
 
</div></dd></dl>
第656行: 第565行:
 
<div id="datedetailview" class="section">
 
<div id="datedetailview" class="section">
  
== <code>DateDetailView</code> ==
+
== DateDetailView ==
  
 
<dl>
 
<dl>
<dt>''class'' <code>DateDetailView</code></dt>
+
<dt>''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">DateDetailView</span></span></dt>
<dd><p>A page representing an individual object. If the object has a date value in
+
<dd><p>代表单个对象的页面。 如果对象将来有日期值,视图默认会抛出 404 错误,除非你将 <code>allow_future</code> 设置为 <code>True</code></p>
the future, the view will throw a 404 error by default, unless you set
+
<p>'''祖先 (MRO)'''</p>
<code>allow_future</code> to <code>True</code>.</p>
 
<p>'''Ancestors (MRO)'''</p>
 
 
<ul>
 
<ul>
 
<li><p>[[../mixins-single-object#django.views.generic.detail|<code>django.views.generic.detail.SingleObjectTemplateResponseMixin</code>]]</p></li>
 
<li><p>[[../mixins-single-object#django.views.generic.detail|<code>django.views.generic.detail.SingleObjectTemplateResponseMixin</code>]]</p></li>
第672行: 第579行:
 
<li><p>[[../mixins-date-based#django.views.generic.dates|<code>django.views.generic.dates.DayMixin</code>]]</p></li>
 
<li><p>[[../mixins-date-based#django.views.generic.dates|<code>django.views.generic.dates.DayMixin</code>]]</p></li>
 
<li><p>[[../mixins-date-based#django.views.generic.dates|<code>django.views.generic.dates.DateMixin</code>]]</p></li>
 
<li><p>[[../mixins-date-based#django.views.generic.dates|<code>django.views.generic.dates.DateMixin</code>]]</p></li>
<li><p><code>django.views.generic.detail.BaseDetailView</code></p></li>
+
<li><p>[[../generic-display#django.views.generic.detail|<code>django.views.generic.detail.BaseDetailView</code>]]</p></li>
 
<li><p>[[../mixins-single-object#django.views.generic.detail|<code>django.views.generic.detail.SingleObjectMixin</code>]]</p></li>
 
<li><p>[[../mixins-single-object#django.views.generic.detail|<code>django.views.generic.detail.SingleObjectMixin</code>]]</p></li>
 
<li><p>[[../base#django.views.generic.base|<code>django.views.generic.base.View</code>]]</p></li></ul>
 
<li><p>[[../base#django.views.generic.base|<code>django.views.generic.base.View</code>]]</p></li></ul>
  
<p>'''Context'''</p>
+
<p>'''语境'''</p>
 
<ul>
 
<ul>
<li><p>Includes the single object associated with the <code>model</code> specified in
+
<li><p>包括与 <code>DateDetailView</code> 中指定的 <code>model</code> 关联的单个对象。</p></li></ul>
the <code>DateDetailView</code>.</p></li></ul>
 
  
<p>'''Notes'''</p>
+
<p>'''笔记'''</p>
 
<ul>
 
<ul>
<li><p>Uses a default <code>template_name_suffix</code> of <code>_detail</code>.</p></li></ul>
+
<li><p>使用 <code>_detail</code> 的默认 <code>template_name_suffix</code></p></li></ul>
  
<p>'''Example myapp/urls.py''':</p>
+
<p>'''示例 myapp/urls.py'''</p>
 
<div class="highlight-default notranslate">
 
<div class="highlight-default notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>from django.urls import path
+
<syntaxhighlight lang="python">from django.urls import path
 
from django.views.generic.dates import DateDetailView
 
from django.views.generic.dates import DateDetailView
  
 
urlpatterns = [
 
urlpatterns = [
     path('&lt;int:year&gt;/&lt;str:month&gt;/&lt;int:day&gt;/&lt;int:pk&gt;/',
+
     path('<int:year>/<str:month>/<int:day>/<int:pk>/',
         DateDetailView.as_view(model=Article, date_field=&quot;pub_date&quot;),
+
         DateDetailView.as_view(model=Article, date_field="pub_date"),
         name=&quot;archive_date_detail&quot;),
+
         name="archive_date_detail"),
]</pre>
+
]</syntaxhighlight>
  
 
</div>
 
</div>
  
 
</div>
 
</div>
<p>'''Example myapp/article_detail.html''':</p>
+
<p>'''示例 myapp/article_detail.html'''</p>
 
<div class="highlight-html+django notranslate">
 
<div class="highlight-html+django notranslate">
  
 
<div class="highlight">
 
<div class="highlight">
  
<pre>&lt;h1&gt;{{ object.title }}&lt;/h1&gt;</pre>
+
<syntaxhighlight lang="html+django"><h1>{{ object.title }}</h1></syntaxhighlight>
  
 
</div>
 
</div>
第715行: 第621行:
 
<div class="admonition note">
 
<div class="admonition note">
  
注解
+
笔记
  
All of the generic views listed above have matching <code>Base</code> views that
+
上面列出的所有通用视图都有匹配的 <code>Base</code> 视图,不同之处仅在于它们不包括 [[../mixins-multiple-object#django.views.generic.list|MultipleObjectTemplateResponseMixin]](对于存档视图)或 [[../mixins-single-object#django.views.generic.detail|SingleObjectTemplateResponseMixin]](对于[[../flattened-index#DateDetailView|DateDetailView]])
only differ in that they do not include the
 
[[../mixins-multiple-object#django.views.generic.list|<code>MultipleObjectTemplateResponseMixin</code>]]
 
(for the archive views) or
 
[[../mixins-single-object#django.views.generic.detail|<code>SingleObjectTemplateResponseMixin</code>]]
 
(for the [[../flattened-index#DateDetailView|<code>DateDetailView</code>]]):
 
  
; ''class'' <code>BaseArchiveIndexView</code>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">BaseArchiveIndexView</span></span>
 
:  
 
:  
  
; ''class'' <code>BaseYearArchiveView</code>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">BaseYearArchiveView</span></span>
 
:  
 
:  
  
; ''class'' <code>BaseMonthArchiveView</code>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">BaseMonthArchiveView</span></span>
 
:  
 
:  
  
; ''class'' <code>BaseWeekArchiveView</code>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">BaseWeekArchiveView</span></span>
 
:  
 
:  
  
; ''class'' <code>BaseDayArchiveView</code>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">BaseDayArchiveView</span></span>
 
:  
 
:  
  
; ''class'' <code>BaseTodayArchiveView</code>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">BaseTodayArchiveView</span></span>
 
:  
 
:  
  
; ''class'' <code>BaseDateDetailView</code>
+
; ''<span class="pre">class</span>'' <span class="sig-name descname"><span class="pre">BaseDateDetailView</span></span>
 
:  
 
:  
  
第749行: 第650行:
  
 
</div>
 
</div>
 +
 +
</div>
 +
<div class="clearer">
 +
 +
  
 
</div>
 
</div>
  
[[Category:Django 3.1.x 中文文档]]
+
[[Category:Django 3.1.x 文档]]

2021年10月31日 (日) 04:13的最新版本

通用日期视图

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

class ArchiveIndexView

按日期显示“最新”对象的顶级索引页面。 除非您将 allow_future 设置为 True,否则不包括日期在 future 中的对象。

祖先 (MRO)

语境

除了 django.views.generic.list.MultipleObjectMixin(通过 django.views.generic.dates.BaseDateListView)提供的上下文,模板的上下文将是:

  • date_list:一个 QuerySet 对象,包含根据 queryset 具有可用对象的所有年份,以降序表示为 datetime.datetime 对象。

笔记

  • 使用 latest 的默认 context_object_name

  • 使用 _archive 的默认 template_name_suffix

  • 默认为按年提供 date_list,但可以使用属性 date_list_period 将其更改为月或日。 这也适用于所有子类视图。

示例 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"),
]

示例 myapp/article_archive.html

<ul>
    {% for article in latest %}
        <li>{{ article.pub_date }}: {{ article.title }}</li>
    {% endfor %}
</ul>

这将输出所有文章。


YearArchiveView

class YearArchiveView

显示给定年份中所有可用月份的年度存档页面。 除非将 allow_future 设置为 True,否则不会显示日期在 future 中的对象。

祖先 (MRO)

make_object_list

一个布尔值,指定是否检索今年的完整对象列表并将它们传递给模板。 如果 True,对象列表将可用于上下文。 如果是 False,则 None 查询集将用作对象列表。 默认情况下,这是 False

get_make_object_list()

确定对象列表是否将作为上下文的一部分返回。 默认返回 make_object_list

语境

除了 django.views.generic.list.MultipleObjectMixin(通过 django.views.generic.dates.BaseDateListView)提供的上下文,模板的上下文将是:

  • date_list:一个 QuerySet 对象,包含根据 queryset 具有可用对象的所有月份,表示为 datetime.datetime 对象,按升序排列。

  • year:代表给定年份的 date 对象。

  • next_year:根据 allow_emptyallow_future,代表下一年第一天的 date 对象。

  • previous_year:根据 allow_emptyallow_future,表示前一年第一天的 date 对象。

笔记

  • 使用 _archive_year 的默认 template_name_suffix

示例 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

示例 myapp/urls.py

from django.urls import path

from myapp.views import ArticleYearArchiveView

urlpatterns = [
    path('<int:year>/',
         ArticleYearArchiveView.as_view(),
         name="article_year_archive"),
]

示例 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

显示给定月份中所有对象的每月存档页面。 除非将 allow_future 设置为 True,否则不会显示日期在 future 中的对象。

祖先 (MRO)

语境

除了 MultipleObjectMixin(通过 BaseDateListView)提供的上下文之外,模板的上下文将是:

  • date_list:一个 QuerySet 对象,包含给定月份中所有有对象可用的天,根据 queryset,表示为 datetime.datetime 对象,按升序排列.

  • month:代表给定月份的 date 对象。

  • next_month:根据 allow_emptyallow_future,表示下个月第一天的 date 对象。

  • previous_month:根据 allow_emptyallow_future,表示上个月第一天的 date 对象。

笔记

  • 使用 _archive_month 的默认 template_name_suffix

示例 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

示例 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"),
]

示例 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

显示给定周内所有对象的每周存档页面。 除非将 allow_future 设置为 True,否则不会显示日期在 future 中的对象。

祖先 (MRO)

语境

除了 MultipleObjectMixin(通过 BaseDateListView)提供的上下文之外,模板的上下文将是:

  • week:表示给定周的第一天的 date 对象。

  • next_week:根据 allow_emptyallow_future,表示下周第一天的 date 对象。

  • previous_week:根据 allow_emptyallow_future,代表上周第一天的 date 对象。

笔记

  • 使用 _archive_week 的默认 template_name_suffix

  • week_format 属性是用于解析周数的 strptime() 格式字符串。 支持以下值:

    • '%U':基于美国的星期系统,星期从星期日开始。 这是默认值。

    • '%W':与 '%U' 类似,只是它假设一周从星期一开始。 这与 ISO 8601 周数不同。

示例 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

示例 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"),
]

示例 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>

在本例中,您将输出周数。 请记住,使用 'W' 格式字符的 :tfilter:`date` 模板过滤器计算的周数并不总是与 strftime() 和 [ X183X] 与 '%W' 格式字符串。 例如,对于 2015 年,:tfilter:`date` 输出的周数比 strftime() 输出的周数高 1。 :tfilter:`date` 中的 '%U' strftime() 格式字符串没有等效项。 因此,您应该避免使用 :tfilter:`date`WeekArchiveView 生成 URL。


DayArchiveView

class DayArchiveView

一天存档页面,显示给定日期内的所有对象。 未来几天会抛出 404 错误,无论未来几天是否存在任何对象,除非您将 allow_future 设置为 True

祖先 (MRO)

语境

除了 MultipleObjectMixin(通过 BaseDateListView)提供的上下文之外,模板的上下文将是:

笔记

  • 使用 _archive_day 的默认 template_name_suffix

示例 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

示例 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"),
]

示例 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

显示 today 的所有对象的日存档页面。 这与 django.views.generic.dates.DayArchiveView 完全相同,除了使用今天的日期而不是 year/month/day论据。

祖先 (MRO)

笔记

  • 使用 _archive_today 的默认 template_name_suffix

示例 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

示例 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

class DateDetailView

代表单个对象的页面。 如果对象将来有日期值,视图默认会抛出 404 错误,除非你将 allow_future 设置为 True

祖先 (MRO)

语境

  • 包括与 DateDetailView 中指定的 model 关联的单个对象。

笔记

  • 使用 _detail 的默认 template_name_suffix

示例 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"),
]

示例 myapp/article_detail.html

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

笔记

上面列出的所有通用视图都有匹配的 Base 视图,不同之处仅在于它们不包括 MultipleObjectTemplateResponseMixin(对于存档视图)或 SingleObjectTemplateResponseMixin(对于DateDetailView):

class BaseArchiveIndexView
class BaseYearArchiveView
class BaseMonthArchiveView
class BaseWeekArchiveView
class BaseDayArchiveView
class BaseTodayArchiveView
class BaseDateDetailView