當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。