-
每周存檔頁麵,顯示給定一周內的所有對象。除非您將
allow_future設置為True,否則不會顯示日期在future中的對象。祖先 (MRO)
django.views.generic.list.MultipleObjectTemplateResponseMixindjango.views.generic.base.TemplateResponseMixindjango.views.generic.dates.BaseWeekArchiveViewdjango.views.generic.dates.YearMixindjango.views.generic.dates.WeekMixindjango.views.generic.dates.BaseDateListView- MultipleObjectMixin
django.views.generic.dates.DateMixin- View
語境
除了
(通過MultipleObjectMixin)提供的上下文之外,模板的上下文將是:BaseDateListViewweek:一個對象,表示給定一周的第一天。datenext_week:根據和allow_empty表示下一周的第一天的allow_future對象。dateprevious_week:根據和allow_empty表示前一周的第一天的allow_future對象。date
注意
-
使用
_archive_week的默認template_name_suffix。 -
week_format屬性是用於解析周數的格式字符串。支持以下值:strptime()-
'%U':基於每周從星期日開始的美國周係統。這是默認值。 -
'%W':類似於'%U',但它假定一周從星期一開始。這與 ISO 8601 周數不同。 -
'%V':ISO 8601 周數,該周從星期一開始。Django 3.2 中的新函數:添加了對
'%V'周格式的支持。
-
示例 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'格式字符的模板過濾器計算的周數並不總是與使用date'%W'格式字符串的和strftime()計算的相同。例如,對於 2015 年,strptime()輸出的周數比date輸出的周數高 1。strftime()中的date'%U'格式字符串沒有等效項。因此,您應該避免使用strftime()為dateWeekArchiveView生成 URL。
本文介紹 django.views.generic.dates.WeekArchiveView 的用法。
聲明
class WeekArchiveView
相關用法
- Python Wand negate()用法及代碼示例
- Python Wand wavelet_denoise()用法及代碼示例
- Python Wand local_contrast()用法及代碼示例
- Python Wand sample()用法及代碼示例
- Python Wand deskew()用法及代碼示例
- Python Wand color()用法及代碼示例
- Python Wand implode()用法及代碼示例
- Python Wand adaptive_blur()用法及代碼示例
- Python Wand unsharp_mask()用法及代碼示例
- Python Wand gaussian_blur()用法及代碼示例
- Python Wand polaroid()用法及代碼示例
- Python Django WKBWriter.write_hex用法及代碼示例
- Python Wand adaptive_sharpen()用法及代碼示例
- Python Wand posterize()用法及代碼示例
- Python Wand path_move()用法及代碼示例
- Python Wand transform()用法及代碼示例
- Python Wand shave()用法及代碼示例
- Python Django WKTWriter.trim用法及代碼示例
- Python Wand function()用法及代碼示例
- Python Wand level()用法及代碼示例
- Python Wand swirl()用法及代碼示例
- Python Wand canny()用法及代碼示例
- Python Django WKBReader用法及代碼示例
- Python Wand rectangle()用法及代碼示例
- Python Wand composite()用法及代碼示例
注:本文由純淨天空篩選整理自djangoproject.com大神的英文原創作品 django.views.generic.dates.WeekArchiveView。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
