当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python Django MonthArchiveView用法及代码示例


本文介绍 django.views.generic.dates.MonthArchiveView 的用法。

声明

class MonthArchiveView

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

祖先 (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.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>

相关用法


注:本文由纯净天空筛选整理自djangoproject.com大神的英文原创作品 django.views.generic.dates.MonthArchiveView。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。