generic-date-based.txt 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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.core.urlresolvers 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
  37. :meth:`DateQuerySet<django.db.models.query.QuerySet.dates>` object
  38. containing all years that have objects available according to
  39. ``queryset``, represented as
  40. :class:`datetime.datetime<python:datetime.datetime>` objects, in
  41. descending order.
  42. **Notes**
  43. * Uses a default ``context_object_name`` of ``latest``.
  44. * Uses a default ``template_name_suffix`` of ``_archive``.
  45. * Defaults to providing ``date_list`` by year, but this can be altered to
  46. month or day using the attribute ``date_list_period``. This also applies
  47. to all subclass views.
  48. **Example myapp/urls.py**::
  49. from django.conf.urls import url
  50. from django.views.generic.dates import ArchiveIndexView
  51. from myapp.models import Article
  52. urlpatterns = [
  53. url(r'^archive/$',
  54. ArchiveIndexView.as_view(model=Article, date_field="pub_date"),
  55. name="article_archive"),
  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
  95. :meth:`DateQuerySet<django.db.models.query.QuerySet.dates>` object
  96. containing all months that have objects available according to
  97. ``queryset``, represented as
  98. :class:`datetime.datetime<python:datetime.datetime>` objects, in
  99. ascending order.
  100. * ``year``: A :class:`~datetime.date` object
  101. representing the given year.
  102. * ``next_year``: A :class:`~datetime.date` object
  103. representing the first day of the next year, according to
  104. :attr:`~BaseDateListView.allow_empty` and
  105. :attr:`~DateMixin.allow_future`.
  106. * ``previous_year``: A :class:`~datetime.date` object
  107. representing the first day of the previous year, according to
  108. :attr:`~BaseDateListView.allow_empty` and
  109. :attr:`~DateMixin.allow_future`.
  110. **Notes**
  111. * Uses a default ``template_name_suffix`` of ``_archive_year``.
  112. **Example myapp/views.py**::
  113. from django.views.generic.dates import YearArchiveView
  114. from myapp.models import Article
  115. class ArticleYearArchiveView(YearArchiveView):
  116. queryset = Article.objects.all()
  117. date_field = "pub_date"
  118. make_object_list = True
  119. allow_future = True
  120. **Example myapp/urls.py**::
  121. from django.conf.urls import url
  122. from myapp.views import ArticleYearArchiveView
  123. urlpatterns = [
  124. url(r'^(?P<year>[0-9]{4})/$',
  125. ArticleYearArchiveView.as_view(),
  126. name="article_year_archive"),
  127. ]
  128. **Example myapp/article_archive_year.html**:
  129. .. code-block:: html+django
  130. <ul>
  131. {% for date in date_list %}
  132. <li>{{ date|date }}</li>
  133. {% endfor %}
  134. </ul>
  135. <div>
  136. <h1>All Articles for {{ year|date:"Y" }}</h1>
  137. {% for obj in object_list %}
  138. <p>
  139. {{ obj.title }} - {{ obj.pub_date|date:"F j, Y" }}
  140. </p>
  141. {% endfor %}
  142. </div>
  143. MonthArchiveView
  144. ----------------
  145. .. class:: MonthArchiveView
  146. A monthly archive page showing all objects in a given month. Objects with a
  147. date in the *future* are not displayed unless you set ``allow_future`` to
  148. ``True``.
  149. **Ancestors (MRO)**
  150. * :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
  151. * :class:`django.views.generic.base.TemplateResponseMixin`
  152. * :class:`django.views.generic.dates.BaseMonthArchiveView`
  153. * :class:`django.views.generic.dates.YearMixin`
  154. * :class:`django.views.generic.dates.MonthMixin`
  155. * :class:`django.views.generic.dates.BaseDateListView`
  156. * :class:`django.views.generic.list.MultipleObjectMixin`
  157. * :class:`django.views.generic.dates.DateMixin`
  158. * :class:`django.views.generic.base.View`
  159. **Context**
  160. In addition to the context provided by
  161. :class:`~django.views.generic.list.MultipleObjectMixin` (via
  162. :class:`~django.views.generic.dates.BaseDateListView`), the template's
  163. context will be:
  164. * ``date_list``: A
  165. :meth:`DateQuerySet<django.db.models.query.QuerySet.dates>` object
  166. containing all days that have objects available in the given month,
  167. according to ``queryset``, represented as
  168. :class:`datetime.datetime<python:datetime.datetime>` objects, in
  169. ascending order.
  170. * ``month``: A :class:`~datetime.date` object
  171. representing the given month.
  172. * ``next_month``: A :class:`~datetime.date` object
  173. representing the first day of the next month, according to
  174. :attr:`~BaseDateListView.allow_empty` and
  175. :attr:`~DateMixin.allow_future`.
  176. * ``previous_month``: A :class:`~datetime.date` object
  177. representing the first day of the previous month, according to
  178. :attr:`~BaseDateListView.allow_empty` and
  179. :attr:`~DateMixin.allow_future`.
  180. **Notes**
  181. * Uses a default ``template_name_suffix`` of ``_archive_month``.
  182. **Example myapp/views.py**::
  183. from django.views.generic.dates import MonthArchiveView
  184. from myapp.models import Article
  185. class ArticleMonthArchiveView(MonthArchiveView):
  186. queryset = Article.objects.all()
  187. date_field = "pub_date"
  188. allow_future = True
  189. **Example myapp/urls.py**::
  190. from django.conf.urls import url
  191. from myapp.views import ArticleMonthArchiveView
  192. urlpatterns = [
  193. # Example: /2012/aug/
  194. url(r'^(?P<year>[0-9]{4})/(?P<month>[-\w]+)/$',
  195. ArticleMonthArchiveView.as_view(),
  196. name="archive_month"),
  197. # Example: /2012/08/
  198. url(r'^(?P<year>[0-9]{4})/(?P<month>[0-9]+)/$',
  199. ArticleMonthArchiveView.as_view(month_format='%m'),
  200. name="archive_month_numeric"),
  201. ]
  202. **Example myapp/article_archive_month.html**:
  203. .. code-block:: html+django
  204. <ul>
  205. {% for article in object_list %}
  206. <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
  207. {% endfor %}
  208. </ul>
  209. <p>
  210. {% if previous_month %}
  211. Previous Month: {{ previous_month|date:"F Y" }}
  212. {% endif %}
  213. {% if next_month %}
  214. Next Month: {{ next_month|date:"F Y" }}
  215. {% endif %}
  216. </p>
  217. WeekArchiveView
  218. ---------------
  219. .. class:: WeekArchiveView
  220. A weekly archive page showing all objects in a given week. Objects with a
  221. date in the *future* are not displayed unless you set ``allow_future`` to
  222. ``True``.
  223. **Ancestors (MRO)**
  224. * :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
  225. * :class:`django.views.generic.base.TemplateResponseMixin`
  226. * :class:`django.views.generic.dates.BaseWeekArchiveView`
  227. * :class:`django.views.generic.dates.YearMixin`
  228. * :class:`django.views.generic.dates.WeekMixin`
  229. * :class:`django.views.generic.dates.BaseDateListView`
  230. * :class:`django.views.generic.list.MultipleObjectMixin`
  231. * :class:`django.views.generic.dates.DateMixin`
  232. * :class:`django.views.generic.base.View`
  233. **Context**
  234. In addition to the context provided by
  235. :class:`~django.views.generic.list.MultipleObjectMixin` (via
  236. :class:`~django.views.generic.dates.BaseDateListView`), the template's
  237. context will be:
  238. * ``week``: A :class:`~datetime.date` object
  239. representing the first day of the given week.
  240. * ``next_week``: A :class:`~datetime.date` object
  241. representing the first day of the next week, according to
  242. :attr:`~BaseDateListView.allow_empty` and
  243. :attr:`~DateMixin.allow_future`.
  244. * ``previous_week``: A :class:`~datetime.date` object
  245. representing the first day of the previous week, according to
  246. :attr:`~BaseDateListView.allow_empty` and
  247. :attr:`~DateMixin.allow_future`.
  248. **Notes**
  249. * Uses a default ``template_name_suffix`` of ``_archive_week``.
  250. **Example myapp/views.py**::
  251. from django.views.generic.dates import WeekArchiveView
  252. from myapp.models import Article
  253. class ArticleWeekArchiveView(WeekArchiveView):
  254. queryset = Article.objects.all()
  255. date_field = "pub_date"
  256. week_format = "%W"
  257. allow_future = True
  258. **Example myapp/urls.py**::
  259. from django.conf.urls import url
  260. from myapp.views import ArticleWeekArchiveView
  261. urlpatterns = [
  262. # Example: /2012/week/23/
  263. url(r'^(?P<year>[0-9]{4})/week/(?P<week>[0-9]+)/$',
  264. ArticleWeekArchiveView.as_view(),
  265. name="archive_week"),
  266. ]
  267. **Example myapp/article_archive_week.html**:
  268. .. code-block:: html+django
  269. <h1>Week {{ week|date:'W' }}</h1>
  270. <ul>
  271. {% for article in object_list %}
  272. <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
  273. {% endfor %}
  274. </ul>
  275. <p>
  276. {% if previous_week %}
  277. Previous Week: {{ previous_week|date:"F Y" }}
  278. {% endif %}
  279. {% if previous_week and next_week %}--{% endif %}
  280. {% if next_week %}
  281. Next week: {{ next_week|date:"F Y" }}
  282. {% endif %}
  283. </p>
  284. In this example, you are outputting the week number. The default
  285. ``week_format`` in the ``WeekArchiveView`` uses week format ``'%U'``
  286. which is based on the United States week system where the week begins on a
  287. Sunday. The ``'%W'`` format uses the ISO week format and its week
  288. begins on a Monday. The ``'%W'`` format is the same in both the
  289. :func:`~time.strftime` and the :tfilter:`date`.
  290. However, the :tfilter:`date` template filter does not have an equivalent
  291. output format that supports the US based week system. The :tfilter:`date`
  292. filter ``'%U'`` outputs the number of seconds since the Unix epoch.
  293. DayArchiveView
  294. --------------
  295. .. class:: DayArchiveView
  296. A day archive page showing all objects in a given day. Days in the future
  297. throw a 404 error, regardless of whether any objects exist for future days,
  298. unless you set ``allow_future`` to ``True``.
  299. **Ancestors (MRO)**
  300. * :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
  301. * :class:`django.views.generic.base.TemplateResponseMixin`
  302. * :class:`django.views.generic.dates.BaseDayArchiveView`
  303. * :class:`django.views.generic.dates.YearMixin`
  304. * :class:`django.views.generic.dates.MonthMixin`
  305. * :class:`django.views.generic.dates.DayMixin`
  306. * :class:`django.views.generic.dates.BaseDateListView`
  307. * :class:`django.views.generic.list.MultipleObjectMixin`
  308. * :class:`django.views.generic.dates.DateMixin`
  309. * :class:`django.views.generic.base.View`
  310. **Context**
  311. In addition to the context provided by
  312. :class:`~django.views.generic.list.MultipleObjectMixin` (via
  313. :class:`~django.views.generic.dates.BaseDateListView`), the template's
  314. context will be:
  315. * ``day``: A :class:`~datetime.date` object
  316. representing the given day.
  317. * ``next_day``: A :class:`~datetime.date` object
  318. representing the next day, according to
  319. :attr:`~BaseDateListView.allow_empty` and
  320. :attr:`~DateMixin.allow_future`.
  321. * ``previous_day``: A :class:`~datetime.date` object
  322. representing the previous day, according to
  323. :attr:`~BaseDateListView.allow_empty` and
  324. :attr:`~DateMixin.allow_future`.
  325. * ``next_month``: A :class:`~datetime.date` object
  326. representing the first day of the next month, according to
  327. :attr:`~BaseDateListView.allow_empty` and
  328. :attr:`~DateMixin.allow_future`.
  329. * ``previous_month``: A :class:`~datetime.date` object
  330. representing the first day of the previous month, according to
  331. :attr:`~BaseDateListView.allow_empty` and
  332. :attr:`~DateMixin.allow_future`.
  333. **Notes**
  334. * Uses a default ``template_name_suffix`` of ``_archive_day``.
  335. **Example myapp/views.py**::
  336. from django.views.generic.dates import DayArchiveView
  337. from myapp.models import Article
  338. class ArticleDayArchiveView(DayArchiveView):
  339. queryset = Article.objects.all()
  340. date_field = "pub_date"
  341. allow_future = True
  342. **Example myapp/urls.py**::
  343. from django.conf.urls import url
  344. from myapp.views import ArticleDayArchiveView
  345. urlpatterns = [
  346. # Example: /2012/nov/10/
  347. url(r'^(?P<year>[0-9]{4})/(?P<month>[-\w]+)/(?P<day>[0-9]+)/$',
  348. ArticleDayArchiveView.as_view(),
  349. name="archive_day"),
  350. ]
  351. **Example myapp/article_archive_day.html**:
  352. .. code-block:: html+django
  353. <h1>{{ day }}</h1>
  354. <ul>
  355. {% for article in object_list %}
  356. <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
  357. {% endfor %}
  358. </ul>
  359. <p>
  360. {% if previous_day %}
  361. Previous Day: {{ previous_day }}
  362. {% endif %}
  363. {% if previous_day and next_day %}--{% endif %}
  364. {% if next_day %}
  365. Next Day: {{ next_day }}
  366. {% endif %}
  367. </p>
  368. TodayArchiveView
  369. ----------------
  370. .. class:: TodayArchiveView
  371. A day archive page showing all objects for *today*. This is exactly the
  372. same as :class:`django.views.generic.dates.DayArchiveView`, except today's
  373. date is used instead of the ``year``/``month``/``day`` arguments.
  374. **Ancestors (MRO)**
  375. * :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
  376. * :class:`django.views.generic.base.TemplateResponseMixin`
  377. * :class:`django.views.generic.dates.BaseTodayArchiveView`
  378. * :class:`django.views.generic.dates.BaseDayArchiveView`
  379. * :class:`django.views.generic.dates.YearMixin`
  380. * :class:`django.views.generic.dates.MonthMixin`
  381. * :class:`django.views.generic.dates.DayMixin`
  382. * :class:`django.views.generic.dates.BaseDateListView`
  383. * :class:`django.views.generic.list.MultipleObjectMixin`
  384. * :class:`django.views.generic.dates.DateMixin`
  385. * :class:`django.views.generic.base.View`
  386. **Notes**
  387. * Uses a default ``template_name_suffix`` of ``_archive_today``.
  388. **Example myapp/views.py**::
  389. from django.views.generic.dates import TodayArchiveView
  390. from myapp.models import Article
  391. class ArticleTodayArchiveView(TodayArchiveView):
  392. queryset = Article.objects.all()
  393. date_field = "pub_date"
  394. allow_future = True
  395. **Example myapp/urls.py**::
  396. from django.conf.urls import url
  397. from myapp.views import ArticleTodayArchiveView
  398. urlpatterns = [
  399. url(r'^today/$',
  400. ArticleTodayArchiveView.as_view(),
  401. name="archive_today"),
  402. ]
  403. .. admonition:: Where is the example template for ``TodayArchiveView``?
  404. This view uses by default the same template as the
  405. :class:`~DayArchiveView`, which is in the previous example. If you need
  406. a different template, set the ``template_name`` attribute to be the
  407. name of the new template.
  408. DateDetailView
  409. --------------
  410. .. class:: DateDetailView
  411. A page representing an individual object. If the object has a date value in
  412. the future, the view will throw a 404 error by default, unless you set
  413. ``allow_future`` to ``True``.
  414. **Ancestors (MRO)**
  415. * :class:`django.views.generic.detail.SingleObjectTemplateResponseMixin`
  416. * :class:`django.views.generic.base.TemplateResponseMixin`
  417. * :class:`django.views.generic.dates.BaseDateDetailView`
  418. * :class:`django.views.generic.dates.YearMixin`
  419. * :class:`django.views.generic.dates.MonthMixin`
  420. * :class:`django.views.generic.dates.DayMixin`
  421. * :class:`django.views.generic.dates.DateMixin`
  422. * ``django.views.generic.detail.BaseDetailView``
  423. * :class:`django.views.generic.detail.SingleObjectMixin`
  424. * :class:`django.views.generic.base.View`
  425. **Context**
  426. * Includes the single object associated with the ``model`` specified in
  427. the ``DateDetailView``.
  428. **Notes**
  429. * Uses a default ``template_name_suffix`` of ``_detail``.
  430. **Example myapp/urls.py**::
  431. from django.conf.urls import url
  432. from django.views.generic.dates import DateDetailView
  433. urlpatterns = [
  434. url(r'^(?P<year>[0-9]{4})/(?P<month>[-\w]+)/(?P<day>[0-9]+)/(?P<pk>[0-9]+)/$',
  435. DateDetailView.as_view(model=Article, date_field="pub_date"),
  436. name="archive_date_detail"),
  437. ]
  438. **Example myapp/article_detail.html**:
  439. .. code-block:: html+django
  440. <h1>{{ object.title }}</h1>
  441. .. note::
  442. All of the generic views listed above have matching ``Base`` views that
  443. only differ in that they do not include the
  444. :class:`~django.views.generic.list.MultipleObjectTemplateResponseMixin`
  445. (for the archive views) or
  446. :class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`
  447. (for the :class:`DateDetailView`):
  448. .. class:: BaseArchiveIndexView
  449. .. class:: BaseYearArchiveView
  450. .. class:: BaseMonthArchiveView
  451. .. class:: BaseWeekArchiveView
  452. .. class:: BaseDayArchiveView
  453. .. class:: BaseTodayArchiveView
  454. .. class:: BaseDateDetailView