Django 1.8.2.dev20150513143415文档

通用日期视图

基于日期的通用视图(在django.views.generic.dates中提供)是用于显示基于日期的数据的向下钻取页面的视图。

注意

此页面上的一些示例假设myapp/models.py中的Article模型已定义如下:

from django.db import models
from django.core.urlresolvers 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[source]

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

祖先(MRO)

上下文

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

备注

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

示例myapp / urls.py

from django.conf.urls import url
from django.views.generic.dates import ArchiveIndexView

from myapp.models import Article

urlpatterns = [
    url(r'^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[source]

一个年度归档页面,显示给定年份中的所有可用月份。除非您将allow_future设置为True,否则不会显示未来中日期的对象。

祖先(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)提供的上下文,模板的上下文将是:

备注

  • 使用_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.conf.urls import url

from myapp.views import ArticleYearArchiveView

urlpatterns = [
    url(r'^(?P<year>[0-9]{4})/$',
        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[source]

显示给定月份中所有对象的月度归档页面。除非您将allow_future设置为True,否则不会显示未来中日期的对象。

祖先(MRO)

上下文

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

备注

  • 使用_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.conf.urls import url

from myapp.views import ArticleMonthArchiveView

urlpatterns = [
    # Example: /2012/aug/
    url(r'^(?P<year>[0-9]{4})/(?P<month>[-\w]+)/$',
        ArticleMonthArchiveView.as_view(),
        name="archive_month"),
    # Example: /2012/08/
    url(r'^(?P<year>[0-9]{4})/(?P<month>[0-9]+)/$',
        ArticleMonthArchiveView.as_view(month_format='%m'),
        name="archive_month_numeric"),
]

示例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[source]

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

祖先(MRO)

上下文

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

备注

  • 使用_archive_week的默认template_name_suffix

示例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.conf.urls import url

from myapp.views import ArticleWeekArchiveView

urlpatterns = [
    # Example: /2012/week/23/
    url(r'^(?P<year>[0-9]{4})/week/(?P<week>[0-9]+)/$',
        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:"F Y" }}
    {% endif %}
    {% if previous_week and next_week %}--{% endif %}
    {% if next_week %}
        Next week: {{ next_week|date:"F Y" }}
    {% endif %}
</p>

在此示例中,您输出的是周数。WeekArchiveView中的默认week_format使用周格式'%U',其基于美国周系统,其中周从星期日开始。'%W'格式使用ISO周格式,其周数从星期一开始。strftime()date中的'%W'格式相同。

但是,date模板过滤器没有支持基于美国星期系统的等效输出格式。date过滤器'%U'输出自Unix纪元以来的秒数。

DayArchiveView

class DayArchiveView[source]

显示指定日期内所有对象的日期归档页面。除非您将allow_future设置为True,否则以后的日期都会抛出404错误,而不管以后是否存在任何对象。

祖先(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.conf.urls import url

from myapp.views import ArticleDayArchiveView

urlpatterns = [
    # Example: /2012/nov/10/
    url(r'^(?P<year>[0-9]{4})/(?P<month>[-\w]+)/(?P<day>[0-9]+)/$',
        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[source]

显示今天的所有对象的日归档页面。This is exactly the same as django.views.generic.dates.DayArchiveView, except today’s date is used instead of the year/month/day arguments.

祖先(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.conf.urls import url

from myapp.views import ArticleTodayArchiveView

urlpatterns = [
    url(r'^today/$',
        ArticleTodayArchiveView.as_view(),
        name="archive_today"),
]

TodayArchiveView的示例模板在哪里?

此视图默认使用与上一个示例中的DayArchiveView相同的模板。如果您需要其他模板,请将template_name属性设置为新模板的名称。

DateDetailView

class DateDetailView[source]

表示个别对象的网页。如果对象未来有日期值,默认情况下视图将抛出404错误,除非您将allow_future设置为True

祖先(MRO)

上下文

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

备注

  • 使用_detail的默认template_name_suffix

示例myapp / urls.py

from django.conf.urls import url
from django.views.generic.dates import DateDetailView

urlpatterns = [
    url(r'^(?P<year>[0-9]{4})/(?P<month>[-\w]+)/(?P<day>[0-9]+)/(?P<pk>[0-9]+)/$',
        DateDetailView.as_view(model=Article, date_field="pub_date"),
        name="archive_date_detail"),
]

示例myapp / article_detail.html

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

注意

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[source]
class BaseYearArchiveView[source]
class BaseMonthArchiveView[source]
class BaseWeekArchiveView[source]
class BaseDayArchiveView[source]
class BaseTodayArchiveView[source]
class BaseDateDetailView[source]