通用日期视图

通用日期视图是用来显示基于日期数据的页面,一些视图在博客系统中非常有用,例如获取某个日期下的全部文章列表

ArchiveIndexView

引入

from django.views.generic.dates import ArchiveIndexView

介绍

按照日期显示“最新”对象的顶级索引页,不包括在当前时间以后的对象,除非设置allow_futureTrue

祖先

  • 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</a>

上下文

上下文表示其可传递到模板里使用

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

笔记

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

示例

# 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"),
]
<!-- article_archive.html -->
<ul>
    {% for article in latest %}
        <li>{{ article.pub_date }}: {{ article.title }}</li>
    {% endfor %}
</ul>

YearArchiveView

引入

from django.views.generic.dates import YearArchiveView

介绍

一个年度归档页面,显示给定年份中的所有可用月份的对象。不包括在当前时间以后的对象,除非设置allow_futureTrue

祖先

  • 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 一个布尔值,指定是否检索今年的完整对象列表,并将它们传递给模板。如果 True,对象的列表object_list将可用于上下文。如果是 False,则 None 查询集将用作对象列表。默认情况下,这是 False。
  • get_make_object_list() 确定对象列表是否将作为上下文的一部分返回。默认情况下返回 make_object_list。

上下文

  • date_list: 包含根据 queryset 具有可用对象的所有月份的 QuerySet 对象,以升序排列表示为 datetime.datetime 对象。
  • year: 表示给定年份的 date 对象.
  • next_year: 表示下一年第一天的 date 对象,根据 allow_empty 和 allow_future.
  • previous_year:表示上一年第一天的 date 对象,根据 allow_empty 和 allow_future。

笔记

  • 使用 _archive_year 的默认 template_name_suffix.

示例

# 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
# urls.py
from django.urls import path

from myapp.views import ArticleYearArchiveView

urlpatterns = [
    path('<int:year>/',
        ArticleYearArchiveView.as_view(),
        name="article_year_archive"),
]
<!-- article_archive_year.htm -->
<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

引入

from django.views.generic.dates import MonthArchiveView

介绍

显示给定月份中所有对象的月度归档页面。不包括在当前时间以后的对象,除非设置allow_futureTrue

祖先

  • 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

上下文

  • date_list: 根据 queryset,包含在给定月份中具有可用对象的所有天的 QuerySet 对象,以升序表示为 datetime.datetime 对象。
  • month: 表示给定月份的 date 对象.
  • next_month: 表示下个月第一天的 date 对象,根据 allow_empty 和 allow_future。
  • previous_month: 表示前一个月第一天的 date 对象,根据 allow_empty 和 allow_future。

笔记

  • 使用 _archive_month 的默认template_name_suffix.

示例

# 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
# urls.py
from django.urls import path

from myapp.views import ArticleMonthArchiveView

urlpatterns = [
    # Example: /2012/aug/
    path('<int:year>/<str:month>/', ArticleMonthArchiveView.as_view(),
        name="archive_month"),
    # Example: /2012/08/
    path('<int:year><int:month>/',
        ArticleMonthArchiveView.as_view(month_format='%m'),
        name="archive_month_numeric"),
]
<!-- 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>

WeekMixin

引入

from django.views.generic.dates import WeekMixin

介绍

可用于检索和提供日期的周组件的解析信息的mixin

方法和属性

  • week_format:在解析星期时使用的 strftime() 格式。默认情况下,这是 '%U',这意味着星期从星期日开始。如果您的星期从星期一开始,请将其设置为 '%W'。
  • week:可选的 星期的值,作为字符串。默认情况下,设置为 None,这意味着将使用其他方法确定星期。
  • get_week_format():返回在解析星期时要使用的 strftime() 格式。默认返回 week_format。
  • get_week(): 返回此视图将以字符串形式显示数据的星期。尝试以下来源,按顺序:

    1. WeekMixin.week 属性的值。
    2. 在URL模式中捕获的 week 参数的值。
    3. week GET 查询参数的值。
  • get_next_week(date): 返回包含所提供日期后一周中第一天的日期对象。此函数还可以返回 None 或引发 Http404 异常,具体取决于 allow_empty 和 allow_future 的值。
  • get_prev_week(date): 返回包含所提供日期前一周的第一天的日期对象。此函数还可以返回 None 或引发 Http404 异常,具体取决于 allow_empty 和 allow_future 的值。

DateMixin

引入

from django.views.generic.dates import DateMixin

介绍

一个mixin类,为所有基于日期的视图提供公共行为。

方法和属性

  • date_filed: QuerySet模型中的DateFieldDateTimeField的名称,基于日期的归档应使用该名称来确定要在页面上显示的对象的列表。当启用时区支持并且date_fieldDateTimeField 时,假定日期位于当前时区。否则,查询集可以包括来自最终用户的时区中的前一天或次日的对象。
  • allow_future: 指定是否在此页面上包含“未来”对象的布尔值,其中“未来”表示 date_field 中指定的字段大于当前日期/时间的对象。默认情况下,这是 False。
  • get_date_field():返回包含此视图将操作的日期数据的字段的名称。默认情况下返回 date_field
  • get_allow_future(): 确定是否在此页面上包含“未来”对象,其中“未来”表示 date_field中指定的字段大于当前日期/时间的对象。默认返回allow_future

BaseDateListView

引入

from django.views.generic.dates import BaseDateListView

介绍

为所有基于日期的视图提供常见行为的基类。通常不会有一个原因来实例化 BaseDateListView;实例化其中一个子类。

当该视图(及其子类)正在执行时,self.object_list 将包含视图正在操作的对象列表,self.date_list 将包含数据可用的日期列表。

方法和属性

  • allow_empty: 一个布尔值,指定如果没有对象可用,是否显示页面。如果这是 True,没有可用的对象,视图将显示一个空页面,而不是返回404。
  • date_list_period: 可选的 定义date_list的聚合周期的字符串。它必须是 'year' (默认值),'month' 或 'day' 之一。
  • get_dated_items(): 返回包含(date_list,object_list,extra_context)的3元组。
  • get_dated_queryset(**lookup): 返回一个查询集,使用由 lookup 定义的查询参数进行过滤。强制执行对查询集的任何限制,例如 allow_empty 和 allow_future。
  • get_date_list_period(): 返回 date_list 的聚合周期。默认返回 date_list_period
  • get_date_list(queryset, date_type=None, ordering='ASC'): 返回 queryset类型包含条目的date_type类型的日期列表。例如,get_date_list(qs, 'year') 将返回 qs 具有条目的年份列表。如果不提供 date_type,则使用 get_date_list_period()的结果。 date_type 和 ordering 简单地传递到 QuerySet.dates()
Copyright © itrunner.cn 2020 all right reserved,powered by Gitbook该文章修订时间: 2022-08-28 07:44:16

results matching ""

    No results matching ""