2
0

generic-date-based.txt 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. ==================
  2. Generic date views
  3. ==================
  4. .. module:: django.views.generic.dates
  5. Date-based generic views, provided in :mod:`django.views.generic.dates`, are
  6. views for displaying drilldown pages for date-based data.
  7. .. note::
  8. Some of the examples on this page assume that an ``Article`` model has been
  9. defined as follows in ``myapp/models.py``::
  10. from django.db import models
  11. from django.urls import reverse
  12. class Article(models.Model):
  13. title = models.CharField(max_length=200)
  14. pub_date = models.DateField()
  15. def get_absolute_url(self):
  16. return reverse("article-detail", kwargs={"pk": self.pk})
  17. ``ArchiveIndexView``
  18. ====================
  19. .. class:: ArchiveIndexView
  20. A top-level index page showing the "latest" objects, by date. Objects with
  21. a date in the *future* are not included unless you set ``allow_future`` to
  22. ``True``.
  23. **Ancestors (MRO)**
  24. * :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
  25. * :class:`django.views.generic.base.TemplateResponseMixin`
  26. * :class:`django.views.generic.dates.BaseArchiveIndexView`
  27. * :class:`django.views.generic.dates.BaseDateListView`
  28. * :class:`django.views.generic.list.MultipleObjectMixin`
  29. * :class:`django.views.generic.dates.DateMixin`
  30. * :class:`django.views.generic.base.View`
  31. **Context**
  32. In addition to the context provided by
  33. :class:`django.views.generic.list.MultipleObjectMixin` (via
  34. :class:`django.views.generic.dates.BaseDateListView`), the template's
  35. context will be:
  36. * ``date_list``: A :meth:`QuerySet <django.db.models.query.QuerySet.dates>`
  37. object containing all years that have objects available according to
  38. ``queryset``, represented as :class:`datetime.datetime` objects, in
  39. descending order.
  40. **Notes**
  41. * Uses a default ``context_object_name`` of ``latest``.
  42. * Uses a default ``template_name_suffix`` of ``_archive``.
  43. * Defaults to providing ``date_list`` by year, but this can be altered to
  44. month or day using the attribute ``date_list_period``. This also applies
  45. to all subclass views.
  46. **Example myapp/urls.py**::
  47. from django.urls import path
  48. from django.views.generic.dates import ArchiveIndexView
  49. from myapp.models import Article
  50. urlpatterns = [
  51. path(
  52. "archive/",
  53. ArchiveIndexView.as_view(model=Article, date_field="pub_date"),
  54. name="article_archive",
  55. ),
  56. ]
  57. **Example myapp/article_archive.html**:
  58. .. code-block:: html+django
  59. <ul>
  60. {% for article in latest %}
  61. <li>{{ article.pub_date }}: {{ article.title }}</li>
  62. {% endfor %}
  63. </ul>
  64. This will output all articles.
  65. ``YearArchiveView``
  66. ===================
  67. .. class:: YearArchiveView
  68. A yearly archive page showing all available months in a given year. Objects
  69. with a date in the *future* are not displayed unless you set
  70. ``allow_future`` to ``True``.
  71. **Ancestors (MRO)**
  72. * :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
  73. * :class:`django.views.generic.base.TemplateResponseMixin`
  74. * :class:`django.views.generic.dates.BaseYearArchiveView`
  75. * :class:`django.views.generic.dates.YearMixin`
  76. * :class:`django.views.generic.dates.BaseDateListView`
  77. * :class:`django.views.generic.list.MultipleObjectMixin`
  78. * :class:`django.views.generic.dates.DateMixin`
  79. * :class:`django.views.generic.base.View`
  80. .. attribute:: make_object_list
  81. A boolean specifying whether to retrieve the full list of objects for
  82. this year and pass those to the template. If ``True``, the list of
  83. objects will be made available to the context. If ``False``, the
  84. ``None`` queryset will be used as the object list. By default, this is
  85. ``False``.
  86. .. method:: get_make_object_list()
  87. Determine if an object list will be returned as part of the context.
  88. Returns :attr:`~YearArchiveView.make_object_list` by default.
  89. **Context**
  90. In addition to the context provided by
  91. :class:`django.views.generic.list.MultipleObjectMixin` (via
  92. :class:`django.views.generic.dates.BaseDateListView`), the template's
  93. context will be:
  94. * ``date_list``: A :meth:`QuerySet <django.db.models.query.QuerySet.dates>`
  95. object containing all months that have objects available according to
  96. ``queryset``, represented as :class:`datetime.datetime` objects, in
  97. ascending order.
  98. * ``year``: A :class:`~datetime.date` object
  99. representing the given year.
  100. * ``next_year``: A :class:`~datetime.date` object
  101. representing the first day of the next year, according to
  102. :attr:`~BaseDateListView.allow_empty` and
  103. :attr:`~DateMixin.allow_future`.
  104. * ``previous_year``: A :class:`~datetime.date` object
  105. representing the first day of the previous year, according to
  106. :attr:`~BaseDateListView.allow_empty` and
  107. :attr:`~DateMixin.allow_future`.
  108. **Notes**
  109. * Uses a default ``template_name_suffix`` of ``_archive_year``.
  110. **Example myapp/views.py**::
  111. from django.views.generic.dates import YearArchiveView
  112. from myapp.models import Article
  113. class ArticleYearArchiveView(YearArchiveView):
  114. queryset = Article.objects.all()
  115. date_field = "pub_date"
  116. make_object_list = True
  117. allow_future = True
  118. **Example myapp/urls.py**::
  119. from django.urls import path
  120. from myapp.views import ArticleYearArchiveView
  121. urlpatterns = [
  122. path("<int:year>/", ArticleYearArchiveView.as_view(), name="article_year_archive"),
  123. ]
  124. **Example myapp/article_archive_year.html**:
  125. .. code-block:: html+django
  126. <ul>
  127. {% for date in date_list %}
  128. <li>{{ date|date }}</li>
  129. {% endfor %}
  130. </ul>
  131. <div>
  132. <h1>All Articles for {{ year|date:"Y" }}</h1>
  133. {% for obj in object_list %}
  134. <p>
  135. {{ obj.title }} - {{ obj.pub_date|date:"F j, Y" }}
  136. </p>
  137. {% endfor %}
  138. </div>
  139. ``MonthArchiveView``
  140. ====================
  141. .. class:: MonthArchiveView
  142. A monthly archive page showing all objects in a given month. Objects with a
  143. date in the *future* are not displayed unless you set ``allow_future`` to
  144. ``True``.
  145. **Ancestors (MRO)**
  146. * :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
  147. * :class:`django.views.generic.base.TemplateResponseMixin`
  148. * :class:`django.views.generic.dates.BaseMonthArchiveView`
  149. * :class:`django.views.generic.dates.YearMixin`
  150. * :class:`django.views.generic.dates.MonthMixin`
  151. * :class:`django.views.generic.dates.BaseDateListView`
  152. * :class:`django.views.generic.list.MultipleObjectMixin`
  153. * :class:`django.views.generic.dates.DateMixin`
  154. * :class:`django.views.generic.base.View`
  155. **Context**
  156. In addition to the context provided by
  157. :class:`~django.views.generic.list.MultipleObjectMixin` (via
  158. :class:`~django.views.generic.dates.BaseDateListView`), the template's
  159. context will be:
  160. * ``date_list``: A :meth:`QuerySet <django.db.models.query.QuerySet.dates>`
  161. object containing all days that have objects available in the given month,
  162. according to ``queryset``, represented as :class:`datetime.datetime`
  163. objects, in ascending order.
  164. * ``month``: A :class:`~datetime.date` object
  165. representing the given month.
  166. * ``next_month``: A :class:`~datetime.date` object
  167. representing the first day of the next month, according to
  168. :attr:`~BaseDateListView.allow_empty` and
  169. :attr:`~DateMixin.allow_future`.
  170. * ``previous_month``: A :class:`~datetime.date` object
  171. representing the first day of the previous month, according to
  172. :attr:`~BaseDateListView.allow_empty` and
  173. :attr:`~DateMixin.allow_future`.
  174. **Notes**
  175. * Uses a default ``template_name_suffix`` of ``_archive_month``.
  176. **Example myapp/views.py**::
  177. from django.views.generic.dates import MonthArchiveView
  178. from myapp.models import Article
  179. class ArticleMonthArchiveView(MonthArchiveView):
  180. queryset = Article.objects.all()
  181. date_field = "pub_date"
  182. allow_future = True
  183. **Example myapp/urls.py**::
  184. from django.urls import path
  185. from myapp.views import ArticleMonthArchiveView
  186. urlpatterns = [
  187. # Example: /2012/08/
  188. path(
  189. "<int:year>/<int:month>/",
  190. ArticleMonthArchiveView.as_view(month_format="%m"),
  191. name="archive_month_numeric",
  192. ),
  193. # Example: /2012/aug/
  194. path(
  195. "<int:year>/<str:month>/",
  196. ArticleMonthArchiveView.as_view(),
  197. name="archive_month",
  198. ),
  199. ]
  200. **Example myapp/article_archive_month.html**:
  201. .. code-block:: html+django
  202. <ul>
  203. {% for article in object_list %}
  204. <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
  205. {% endfor %}
  206. </ul>
  207. <p>
  208. {% if previous_month %}
  209. Previous Month: {{ previous_month|date:"F Y" }}
  210. {% endif %}
  211. {% if next_month %}
  212. Next Month: {{ next_month|date:"F Y" }}
  213. {% endif %}
  214. </p>
  215. ``WeekArchiveView``
  216. ===================
  217. .. class:: WeekArchiveView
  218. A weekly archive page showing all objects in a given week. Objects with a
  219. date in the *future* are not displayed unless you set ``allow_future`` to
  220. ``True``.
  221. **Ancestors (MRO)**
  222. * :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
  223. * :class:`django.views.generic.base.TemplateResponseMixin`
  224. * :class:`django.views.generic.dates.BaseWeekArchiveView`
  225. * :class:`django.views.generic.dates.YearMixin`
  226. * :class:`django.views.generic.dates.WeekMixin`
  227. * :class:`django.views.generic.dates.BaseDateListView`
  228. * :class:`django.views.generic.list.MultipleObjectMixin`
  229. * :class:`django.views.generic.dates.DateMixin`
  230. * :class:`django.views.generic.base.View`
  231. **Context**
  232. In addition to the context provided by
  233. :class:`~django.views.generic.list.MultipleObjectMixin` (via
  234. :class:`~django.views.generic.dates.BaseDateListView`), the template's
  235. context will be:
  236. * ``week``: A :class:`~datetime.date` object
  237. representing the first day of the given week.
  238. * ``next_week``: A :class:`~datetime.date` object
  239. representing the first day of the next week, according to
  240. :attr:`~BaseDateListView.allow_empty` and
  241. :attr:`~DateMixin.allow_future`.
  242. * ``previous_week``: A :class:`~datetime.date` object
  243. representing the first day of the previous week, according to
  244. :attr:`~BaseDateListView.allow_empty` and
  245. :attr:`~DateMixin.allow_future`.
  246. **Notes**
  247. * Uses a default ``template_name_suffix`` of ``_archive_week``.
  248. * The ``week_format`` attribute is a :func:`~time.strptime` format string
  249. used to parse the week number. The following values are supported:
  250. * ``'%U'``: Based on the United States week system where the week
  251. begins on Sunday. This is the default value.
  252. * ``'%W'``: Similar to ``'%U'``, except it assumes that the week
  253. begins on Monday. This is not the same as the ISO 8601 week number.
  254. * ``'%V'``: ISO 8601 week number where the week begins on Monday.
  255. **Example myapp/views.py**::
  256. from django.views.generic.dates import WeekArchiveView
  257. from myapp.models import Article
  258. class ArticleWeekArchiveView(WeekArchiveView):
  259. queryset = Article.objects.all()
  260. date_field = "pub_date"
  261. week_format = "%W"
  262. allow_future = True
  263. **Example myapp/urls.py**::
  264. from django.urls import path
  265. from myapp.views import ArticleWeekArchiveView
  266. urlpatterns = [
  267. # Example: /2012/week/23/
  268. path(
  269. "<int:year>/week/<int:week>/",
  270. ArticleWeekArchiveView.as_view(),
  271. name="archive_week",
  272. ),
  273. ]
  274. **Example myapp/article_archive_week.html**:
  275. .. code-block:: html+django
  276. <h1>Week {{ week|date:'W' }}</h1>
  277. <ul>
  278. {% for article in object_list %}
  279. <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
  280. {% endfor %}
  281. </ul>
  282. <p>
  283. {% if previous_week %}
  284. Previous Week: {{ previous_week|date:"W" }} of year {{ previous_week|date:"Y" }}
  285. {% endif %}
  286. {% if previous_week and next_week %}--{% endif %}
  287. {% if next_week %}
  288. Next week: {{ next_week|date:"W" }} of year {{ next_week|date:"Y" }}
  289. {% endif %}
  290. </p>
  291. In this example, you are outputting the week number. Keep in mind that week
  292. numbers computed by the :tfilter:`date` template filter with the ``'W'``
  293. format character are not always the same as those computed by
  294. :func:`~time.strftime` and :func:`~time.strptime` with the ``'%W'`` format
  295. string. For year 2015, for example, week numbers output by :tfilter:`date`
  296. are higher by one compared to those output by :func:`~time.strftime`. There
  297. isn't an equivalent for the ``'%U'`` :func:`~time.strftime` format string
  298. in :tfilter:`date`. Therefore, you should avoid using :tfilter:`date` to
  299. generate URLs for ``WeekArchiveView``.
  300. ``DayArchiveView``
  301. ==================
  302. .. class:: DayArchiveView
  303. A day archive page showing all objects in a given day. Days in the future
  304. throw a 404 error, regardless of whether any objects exist for future days,
  305. unless you set ``allow_future`` to ``True``.
  306. **Ancestors (MRO)**
  307. * :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
  308. * :class:`django.views.generic.base.TemplateResponseMixin`
  309. * :class:`django.views.generic.dates.BaseDayArchiveView`
  310. * :class:`django.views.generic.dates.YearMixin`
  311. * :class:`django.views.generic.dates.MonthMixin`
  312. * :class:`django.views.generic.dates.DayMixin`
  313. * :class:`django.views.generic.dates.BaseDateListView`
  314. * :class:`django.views.generic.list.MultipleObjectMixin`
  315. * :class:`django.views.generic.dates.DateMixin`
  316. * :class:`django.views.generic.base.View`
  317. **Context**
  318. In addition to the context provided by
  319. :class:`~django.views.generic.list.MultipleObjectMixin` (via
  320. :class:`~django.views.generic.dates.BaseDateListView`), the template's
  321. context will be:
  322. * ``day``: A :class:`~datetime.date` object
  323. representing the given day.
  324. * ``next_day``: A :class:`~datetime.date` object
  325. representing the next day, according to
  326. :attr:`~BaseDateListView.allow_empty` and
  327. :attr:`~DateMixin.allow_future`.
  328. * ``previous_day``: A :class:`~datetime.date` object
  329. representing the previous day, according to
  330. :attr:`~BaseDateListView.allow_empty` and
  331. :attr:`~DateMixin.allow_future`.
  332. * ``next_month``: A :class:`~datetime.date` object
  333. representing the first day of the next month, according to
  334. :attr:`~BaseDateListView.allow_empty` and
  335. :attr:`~DateMixin.allow_future`.
  336. * ``previous_month``: A :class:`~datetime.date` object
  337. representing the first day of the previous month, according to
  338. :attr:`~BaseDateListView.allow_empty` and
  339. :attr:`~DateMixin.allow_future`.
  340. **Notes**
  341. * Uses a default ``template_name_suffix`` of ``_archive_day``.
  342. **Example myapp/views.py**::
  343. from django.views.generic.dates import DayArchiveView
  344. from myapp.models import Article
  345. class ArticleDayArchiveView(DayArchiveView):
  346. queryset = Article.objects.all()
  347. date_field = "pub_date"
  348. allow_future = True
  349. **Example myapp/urls.py**::
  350. from django.urls import path
  351. from myapp.views import ArticleDayArchiveView
  352. urlpatterns = [
  353. # Example: /2012/nov/10/
  354. path(
  355. "<int:year>/<str:month>/<int:day>/",
  356. ArticleDayArchiveView.as_view(),
  357. name="archive_day",
  358. ),
  359. ]
  360. **Example myapp/article_archive_day.html**:
  361. .. code-block:: html+django
  362. <h1>{{ day }}</h1>
  363. <ul>
  364. {% for article in object_list %}
  365. <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
  366. {% endfor %}
  367. </ul>
  368. <p>
  369. {% if previous_day %}
  370. Previous Day: {{ previous_day }}
  371. {% endif %}
  372. {% if previous_day and next_day %}--{% endif %}
  373. {% if next_day %}
  374. Next Day: {{ next_day }}
  375. {% endif %}
  376. </p>
  377. ``TodayArchiveView``
  378. ====================
  379. .. class:: TodayArchiveView
  380. A day archive page showing all objects for *today*. This is exactly the
  381. same as :class:`django.views.generic.dates.DayArchiveView`, except today's
  382. date is used instead of the ``year``/``month``/``day`` arguments.
  383. **Ancestors (MRO)**
  384. * :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
  385. * :class:`django.views.generic.base.TemplateResponseMixin`
  386. * :class:`django.views.generic.dates.BaseTodayArchiveView`
  387. * :class:`django.views.generic.dates.BaseDayArchiveView`
  388. * :class:`django.views.generic.dates.YearMixin`
  389. * :class:`django.views.generic.dates.MonthMixin`
  390. * :class:`django.views.generic.dates.DayMixin`
  391. * :class:`django.views.generic.dates.BaseDateListView`
  392. * :class:`django.views.generic.list.MultipleObjectMixin`
  393. * :class:`django.views.generic.dates.DateMixin`
  394. * :class:`django.views.generic.base.View`
  395. **Notes**
  396. * Uses a default ``template_name_suffix`` of ``_archive_today``.
  397. **Example myapp/views.py**::
  398. from django.views.generic.dates import TodayArchiveView
  399. from myapp.models import Article
  400. class ArticleTodayArchiveView(TodayArchiveView):
  401. queryset = Article.objects.all()
  402. date_field = "pub_date"
  403. allow_future = True
  404. **Example myapp/urls.py**::
  405. from django.urls import path
  406. from myapp.views import ArticleTodayArchiveView
  407. urlpatterns = [
  408. path("today/", ArticleTodayArchiveView.as_view(), name="archive_today"),
  409. ]
  410. .. admonition:: Where is the example template for ``TodayArchiveView``?
  411. This view uses by default the same template as the
  412. :class:`~DayArchiveView`, which is in the previous example. If you need
  413. a different template, set the ``template_name`` attribute to be the
  414. name of the new template.
  415. ``DateDetailView``
  416. ==================
  417. .. class:: DateDetailView
  418. A page representing an individual object. If the object has a date value in
  419. the future, the view will throw a 404 error by default, unless you set
  420. ``allow_future`` to ``True``.
  421. **Ancestors (MRO)**
  422. * :class:`django.views.generic.detail.SingleObjectTemplateResponseMixin`
  423. * :class:`django.views.generic.base.TemplateResponseMixin`
  424. * :class:`django.views.generic.dates.BaseDateDetailView`
  425. * :class:`django.views.generic.dates.YearMixin`
  426. * :class:`django.views.generic.dates.MonthMixin`
  427. * :class:`django.views.generic.dates.DayMixin`
  428. * :class:`django.views.generic.dates.DateMixin`
  429. * :class:`django.views.generic.detail.BaseDetailView`
  430. * :class:`django.views.generic.detail.SingleObjectMixin`
  431. * :class:`django.views.generic.base.View`
  432. **Context**
  433. * Includes the single object associated with the ``model`` specified in
  434. the ``DateDetailView``.
  435. **Notes**
  436. * Uses a default ``template_name_suffix`` of ``_detail``.
  437. **Example myapp/urls.py**::
  438. from django.urls import path
  439. from django.views.generic.dates import DateDetailView
  440. urlpatterns = [
  441. path(
  442. "<int:year>/<str:month>/<int:day>/<int:pk>/",
  443. DateDetailView.as_view(model=Article, date_field="pub_date"),
  444. name="archive_date_detail",
  445. ),
  446. ]
  447. **Example myapp/article_detail.html**:
  448. .. code-block:: html+django
  449. <h1>{{ object.title }}</h1>
  450. .. note::
  451. All of the generic views listed above have matching ``Base`` views that
  452. only differ in that they do not include the
  453. :class:`~django.views.generic.list.MultipleObjectTemplateResponseMixin`
  454. (for the archive views) or
  455. :class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`
  456. (for the :class:`DateDetailView`):
  457. .. class:: BaseArchiveIndexView
  458. .. class:: BaseYearArchiveView
  459. .. class:: BaseMonthArchiveView
  460. .. class:: BaseWeekArchiveView
  461. .. class:: BaseDayArchiveView
  462. .. class:: BaseTodayArchiveView
  463. .. class:: BaseDateDetailView