generic-date-based.txt 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636
  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('archive/',
  52. ArchiveIndexView.as_view(model=Article, date_field="pub_date"),
  53. name="article_archive"),
  54. ]
  55. **Example myapp/article_archive.html**:
  56. .. code-block:: html+django
  57. <ul>
  58. {% for article in latest %}
  59. <li>{{ article.pub_date }}: {{ article.title }}</li>
  60. {% endfor %}
  61. </ul>
  62. This will output all articles.
  63. ``YearArchiveView``
  64. ===================
  65. .. class:: YearArchiveView
  66. A yearly archive page showing all available months in a given year. Objects
  67. with a date in the *future* are not displayed unless you set
  68. ``allow_future`` to ``True``.
  69. **Ancestors (MRO)**
  70. * :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
  71. * :class:`django.views.generic.base.TemplateResponseMixin`
  72. * :class:`django.views.generic.dates.BaseYearArchiveView`
  73. * :class:`django.views.generic.dates.YearMixin`
  74. * :class:`django.views.generic.dates.BaseDateListView`
  75. * :class:`django.views.generic.list.MultipleObjectMixin`
  76. * :class:`django.views.generic.dates.DateMixin`
  77. * :class:`django.views.generic.base.View`
  78. .. attribute:: make_object_list
  79. A boolean specifying whether to retrieve the full list of objects for
  80. this year and pass those to the template. If ``True``, the list of
  81. objects will be made available to the context. If ``False``, the
  82. ``None`` queryset will be used as the object list. By default, this is
  83. ``False``.
  84. .. method:: get_make_object_list()
  85. Determine if an object list will be returned as part of the context.
  86. Returns :attr:`~YearArchiveView.make_object_list` by default.
  87. **Context**
  88. In addition to the context provided by
  89. :class:`django.views.generic.list.MultipleObjectMixin` (via
  90. :class:`django.views.generic.dates.BaseDateListView`), the template's
  91. context will be:
  92. * ``date_list``: A :meth:`QuerySet <django.db.models.query.QuerySet.dates>`
  93. object containing all months that have objects available according to
  94. ``queryset``, represented as :class:`datetime.datetime` objects, in
  95. ascending order.
  96. * ``year``: A :class:`~datetime.date` object
  97. representing the given year.
  98. * ``next_year``: A :class:`~datetime.date` object
  99. representing the first day of the next year, according to
  100. :attr:`~BaseDateListView.allow_empty` and
  101. :attr:`~DateMixin.allow_future`.
  102. * ``previous_year``: A :class:`~datetime.date` object
  103. representing the first day of the previous year, according to
  104. :attr:`~BaseDateListView.allow_empty` and
  105. :attr:`~DateMixin.allow_future`.
  106. **Notes**
  107. * Uses a default ``template_name_suffix`` of ``_archive_year``.
  108. **Example myapp/views.py**::
  109. from django.views.generic.dates import YearArchiveView
  110. from myapp.models import Article
  111. class ArticleYearArchiveView(YearArchiveView):
  112. queryset = Article.objects.all()
  113. date_field = "pub_date"
  114. make_object_list = True
  115. allow_future = True
  116. **Example myapp/urls.py**::
  117. from django.urls import path
  118. from myapp.views import ArticleYearArchiveView
  119. urlpatterns = [
  120. path('<int:year>/',
  121. ArticleYearArchiveView.as_view(),
  122. 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('<int:year>/<int:month>/',
  189. ArticleMonthArchiveView.as_view(month_format='%m'),
  190. name="archive_month_numeric"),
  191. # Example: /2012/aug/
  192. path('<int:year>/<str:month>/',
  193. ArticleMonthArchiveView.as_view(),
  194. name="archive_month"),
  195. ]
  196. **Example myapp/article_archive_month.html**:
  197. .. code-block:: html+django
  198. <ul>
  199. {% for article in object_list %}
  200. <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
  201. {% endfor %}
  202. </ul>
  203. <p>
  204. {% if previous_month %}
  205. Previous Month: {{ previous_month|date:"F Y" }}
  206. {% endif %}
  207. {% if next_month %}
  208. Next Month: {{ next_month|date:"F Y" }}
  209. {% endif %}
  210. </p>
  211. ``WeekArchiveView``
  212. ===================
  213. .. class:: WeekArchiveView
  214. A weekly archive page showing all objects in a given week. Objects with a
  215. date in the *future* are not displayed unless you set ``allow_future`` to
  216. ``True``.
  217. **Ancestors (MRO)**
  218. * :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
  219. * :class:`django.views.generic.base.TemplateResponseMixin`
  220. * :class:`django.views.generic.dates.BaseWeekArchiveView`
  221. * :class:`django.views.generic.dates.YearMixin`
  222. * :class:`django.views.generic.dates.WeekMixin`
  223. * :class:`django.views.generic.dates.BaseDateListView`
  224. * :class:`django.views.generic.list.MultipleObjectMixin`
  225. * :class:`django.views.generic.dates.DateMixin`
  226. * :class:`django.views.generic.base.View`
  227. **Context**
  228. In addition to the context provided by
  229. :class:`~django.views.generic.list.MultipleObjectMixin` (via
  230. :class:`~django.views.generic.dates.BaseDateListView`), the template's
  231. context will be:
  232. * ``week``: A :class:`~datetime.date` object
  233. representing the first day of the given week.
  234. * ``next_week``: A :class:`~datetime.date` object
  235. representing the first day of the next week, according to
  236. :attr:`~BaseDateListView.allow_empty` and
  237. :attr:`~DateMixin.allow_future`.
  238. * ``previous_week``: A :class:`~datetime.date` object
  239. representing the first day of the previous week, according to
  240. :attr:`~BaseDateListView.allow_empty` and
  241. :attr:`~DateMixin.allow_future`.
  242. **Notes**
  243. * Uses a default ``template_name_suffix`` of ``_archive_week``.
  244. * The ``week_format`` attribute is a :func:`~time.strptime` format string
  245. used to parse the week number. The following values are supported:
  246. * ``'%U'``: Based on the United States week system where the week
  247. begins on Sunday. This is the default value.
  248. * ``'%W'``: Similar to ``'%U'``, except it assumes that the week
  249. begins on Monday. This is not the same as the ISO 8601 week number.
  250. * ``'%V'``: ISO 8601 week number where the week begins on Monday.
  251. .. versionadded:: 3.2
  252. Support for the ``'%V'`` week format was added.
  253. **Example myapp/views.py**::
  254. from django.views.generic.dates import WeekArchiveView
  255. from myapp.models import Article
  256. class ArticleWeekArchiveView(WeekArchiveView):
  257. queryset = Article.objects.all()
  258. date_field = "pub_date"
  259. week_format = "%W"
  260. allow_future = True
  261. **Example myapp/urls.py**::
  262. from django.urls import path
  263. from myapp.views import ArticleWeekArchiveView
  264. urlpatterns = [
  265. # Example: /2012/week/23/
  266. path('<int:year>/week/<int:week>/',
  267. ArticleWeekArchiveView.as_view(),
  268. name="archive_week"),
  269. ]
  270. **Example myapp/article_archive_week.html**:
  271. .. code-block:: html+django
  272. <h1>Week {{ week|date:'W' }}</h1>
  273. <ul>
  274. {% for article in object_list %}
  275. <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
  276. {% endfor %}
  277. </ul>
  278. <p>
  279. {% if previous_week %}
  280. Previous Week: {{ previous_week|date:"W" }} of year {{ previous_week|date:"Y" }}
  281. {% endif %}
  282. {% if previous_week and next_week %}--{% endif %}
  283. {% if next_week %}
  284. Next week: {{ next_week|date:"W" }} of year {{ next_week|date:"Y" }}
  285. {% endif %}
  286. </p>
  287. In this example, you are outputting the week number. Keep in mind that week
  288. numbers computed by the :tfilter:`date` template filter with the ``'W'``
  289. format character are not always the same as those computed by
  290. :func:`~time.strftime` and :func:`~time.strptime` with the ``'%W'`` format
  291. string. For year 2015, for example, week numbers output by :tfilter:`date`
  292. are higher by one compared to those output by :func:`~time.strftime`. There
  293. isn't an equivalent for the ``'%U'`` :func:`~time.strftime` format string
  294. in :tfilter:`date`. Therefore, you should avoid using :tfilter:`date` to
  295. generate URLs for ``WeekArchiveView``.
  296. ``DayArchiveView``
  297. ==================
  298. .. class:: DayArchiveView
  299. A day archive page showing all objects in a given day. Days in the future
  300. throw a 404 error, regardless of whether any objects exist for future days,
  301. unless you set ``allow_future`` to ``True``.
  302. **Ancestors (MRO)**
  303. * :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
  304. * :class:`django.views.generic.base.TemplateResponseMixin`
  305. * :class:`django.views.generic.dates.BaseDayArchiveView`
  306. * :class:`django.views.generic.dates.YearMixin`
  307. * :class:`django.views.generic.dates.MonthMixin`
  308. * :class:`django.views.generic.dates.DayMixin`
  309. * :class:`django.views.generic.dates.BaseDateListView`
  310. * :class:`django.views.generic.list.MultipleObjectMixin`
  311. * :class:`django.views.generic.dates.DateMixin`
  312. * :class:`django.views.generic.base.View`
  313. **Context**
  314. In addition to the context provided by
  315. :class:`~django.views.generic.list.MultipleObjectMixin` (via
  316. :class:`~django.views.generic.dates.BaseDateListView`), the template's
  317. context will be:
  318. * ``day``: A :class:`~datetime.date` object
  319. representing the given day.
  320. * ``next_day``: A :class:`~datetime.date` object
  321. representing the next day, according to
  322. :attr:`~BaseDateListView.allow_empty` and
  323. :attr:`~DateMixin.allow_future`.
  324. * ``previous_day``: A :class:`~datetime.date` object
  325. representing the previous day, according to
  326. :attr:`~BaseDateListView.allow_empty` and
  327. :attr:`~DateMixin.allow_future`.
  328. * ``next_month``: A :class:`~datetime.date` object
  329. representing the first day of the next month, according to
  330. :attr:`~BaseDateListView.allow_empty` and
  331. :attr:`~DateMixin.allow_future`.
  332. * ``previous_month``: A :class:`~datetime.date` object
  333. representing the first day of the previous month, according to
  334. :attr:`~BaseDateListView.allow_empty` and
  335. :attr:`~DateMixin.allow_future`.
  336. **Notes**
  337. * Uses a default ``template_name_suffix`` of ``_archive_day``.
  338. **Example myapp/views.py**::
  339. from django.views.generic.dates import DayArchiveView
  340. from myapp.models import Article
  341. class ArticleDayArchiveView(DayArchiveView):
  342. queryset = Article.objects.all()
  343. date_field = "pub_date"
  344. allow_future = True
  345. **Example myapp/urls.py**::
  346. from django.urls import path
  347. from myapp.views import ArticleDayArchiveView
  348. urlpatterns = [
  349. # Example: /2012/nov/10/
  350. path('<int:year>/<str:month>/<int:day>/',
  351. ArticleDayArchiveView.as_view(),
  352. name="archive_day"),
  353. ]
  354. **Example myapp/article_archive_day.html**:
  355. .. code-block:: html+django
  356. <h1>{{ day }}</h1>
  357. <ul>
  358. {% for article in object_list %}
  359. <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
  360. {% endfor %}
  361. </ul>
  362. <p>
  363. {% if previous_day %}
  364. Previous Day: {{ previous_day }}
  365. {% endif %}
  366. {% if previous_day and next_day %}--{% endif %}
  367. {% if next_day %}
  368. Next Day: {{ next_day }}
  369. {% endif %}
  370. </p>
  371. ``TodayArchiveView``
  372. ====================
  373. .. class:: TodayArchiveView
  374. A day archive page showing all objects for *today*. This is exactly the
  375. same as :class:`django.views.generic.dates.DayArchiveView`, except today's
  376. date is used instead of the ``year``/``month``/``day`` arguments.
  377. **Ancestors (MRO)**
  378. * :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
  379. * :class:`django.views.generic.base.TemplateResponseMixin`
  380. * :class:`django.views.generic.dates.BaseTodayArchiveView`
  381. * :class:`django.views.generic.dates.BaseDayArchiveView`
  382. * :class:`django.views.generic.dates.YearMixin`
  383. * :class:`django.views.generic.dates.MonthMixin`
  384. * :class:`django.views.generic.dates.DayMixin`
  385. * :class:`django.views.generic.dates.BaseDateListView`
  386. * :class:`django.views.generic.list.MultipleObjectMixin`
  387. * :class:`django.views.generic.dates.DateMixin`
  388. * :class:`django.views.generic.base.View`
  389. **Notes**
  390. * Uses a default ``template_name_suffix`` of ``_archive_today``.
  391. **Example myapp/views.py**::
  392. from django.views.generic.dates import TodayArchiveView
  393. from myapp.models import Article
  394. class ArticleTodayArchiveView(TodayArchiveView):
  395. queryset = Article.objects.all()
  396. date_field = "pub_date"
  397. allow_future = True
  398. **Example myapp/urls.py**::
  399. from django.urls import path
  400. from myapp.views import ArticleTodayArchiveView
  401. urlpatterns = [
  402. path('today/',
  403. ArticleTodayArchiveView.as_view(),
  404. name="archive_today"),
  405. ]
  406. .. admonition:: Where is the example template for ``TodayArchiveView``?
  407. This view uses by default the same template as the
  408. :class:`~DayArchiveView`, which is in the previous example. If you need
  409. a different template, set the ``template_name`` attribute to be the
  410. name of the new template.
  411. ``DateDetailView``
  412. ==================
  413. .. class:: DateDetailView
  414. A page representing an individual object. If the object has a date value in
  415. the future, the view will throw a 404 error by default, unless you set
  416. ``allow_future`` to ``True``.
  417. **Ancestors (MRO)**
  418. * :class:`django.views.generic.detail.SingleObjectTemplateResponseMixin`
  419. * :class:`django.views.generic.base.TemplateResponseMixin`
  420. * :class:`django.views.generic.dates.BaseDateDetailView`
  421. * :class:`django.views.generic.dates.YearMixin`
  422. * :class:`django.views.generic.dates.MonthMixin`
  423. * :class:`django.views.generic.dates.DayMixin`
  424. * :class:`django.views.generic.dates.DateMixin`
  425. * ``django.views.generic.detail.BaseDetailView``
  426. * :class:`django.views.generic.detail.SingleObjectMixin`
  427. * :class:`django.views.generic.base.View`
  428. **Context**
  429. * Includes the single object associated with the ``model`` specified in
  430. the ``DateDetailView``.
  431. **Notes**
  432. * Uses a default ``template_name_suffix`` of ``_detail``.
  433. **Example myapp/urls.py**::
  434. from django.urls import path
  435. from django.views.generic.dates import DateDetailView
  436. urlpatterns = [
  437. path('<int:year>/<str:month>/<int:day>/<int:pk>/',
  438. DateDetailView.as_view(model=Article, date_field="pub_date"),
  439. name="archive_date_detail"),
  440. ]
  441. **Example myapp/article_detail.html**:
  442. .. code-block:: html+django
  443. <h1>{{ object.title }}</h1>
  444. .. note::
  445. All of the generic views listed above have matching ``Base`` views that
  446. only differ in that they do not include the
  447. :class:`~django.views.generic.list.MultipleObjectTemplateResponseMixin`
  448. (for the archive views) or
  449. :class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`
  450. (for the :class:`DateDetailView`):
  451. .. class:: BaseArchiveIndexView
  452. .. class:: BaseYearArchiveView
  453. .. class:: BaseMonthArchiveView
  454. .. class:: BaseWeekArchiveView
  455. .. class:: BaseDayArchiveView
  456. .. class:: BaseTodayArchiveView
  457. .. class:: BaseDateDetailView