generic-date-based.txt 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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 :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.conf.urls import url
  48. from django.views.generic.dates import ArchiveIndexView
  49. from myapp.models import Article
  50. urlpatterns = [
  51. url(r'^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.conf.urls import url
  118. from myapp.views import ArticleYearArchiveView
  119. urlpatterns = [
  120. url(r'^(?P<year>[0-9]{4})/$',
  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.conf.urls import url
  185. from myapp.views import ArticleMonthArchiveView
  186. urlpatterns = [
  187. # Example: /2012/aug/
  188. url(r'^(?P<year>[0-9]{4})/(?P<month>[-\w]+)/$',
  189. ArticleMonthArchiveView.as_view(),
  190. name="archive_month"),
  191. # Example: /2012/08/
  192. url(r'^(?P<year>[0-9]{4})/(?P<month>[0-9]+)/$',
  193. ArticleMonthArchiveView.as_view(month_format='%m'),
  194. name="archive_month_numeric"),
  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. **Example myapp/views.py**::
  245. from django.views.generic.dates import WeekArchiveView
  246. from myapp.models import Article
  247. class ArticleWeekArchiveView(WeekArchiveView):
  248. queryset = Article.objects.all()
  249. date_field = "pub_date"
  250. week_format = "%W"
  251. allow_future = True
  252. **Example myapp/urls.py**::
  253. from django.conf.urls import url
  254. from myapp.views import ArticleWeekArchiveView
  255. urlpatterns = [
  256. # Example: /2012/week/23/
  257. url(r'^(?P<year>[0-9]{4})/week/(?P<week>[0-9]+)/$',
  258. ArticleWeekArchiveView.as_view(),
  259. name="archive_week"),
  260. ]
  261. **Example myapp/article_archive_week.html**:
  262. .. code-block:: html+django
  263. <h1>Week {{ week|date:'W' }}</h1>
  264. <ul>
  265. {% for article in object_list %}
  266. <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
  267. {% endfor %}
  268. </ul>
  269. <p>
  270. {% if previous_week %}
  271. Previous Week: {{ previous_week|date:"F Y" }}
  272. {% endif %}
  273. {% if previous_week and next_week %}--{% endif %}
  274. {% if next_week %}
  275. Next week: {{ next_week|date:"F Y" }}
  276. {% endif %}
  277. </p>
  278. In this example, you are outputting the week number. The default
  279. ``week_format`` in the ``WeekArchiveView`` uses week format ``'%U'``
  280. which is based on the United States week system where the week begins on a
  281. Sunday. The ``'%W'`` format uses the ISO week format and its week
  282. begins on a Monday. The ``'%W'`` format is the same in both the
  283. :func:`~time.strftime` and the :tfilter:`date`.
  284. However, the :tfilter:`date` template filter does not have an equivalent
  285. output format that supports the US based week system. The :tfilter:`date`
  286. filter ``'%U'`` outputs the number of seconds since the Unix epoch.
  287. DayArchiveView
  288. --------------
  289. .. class:: DayArchiveView
  290. A day archive page showing all objects in a given day. Days in the future
  291. throw a 404 error, regardless of whether any objects exist for future days,
  292. unless you set ``allow_future`` to ``True``.
  293. **Ancestors (MRO)**
  294. * :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
  295. * :class:`django.views.generic.base.TemplateResponseMixin`
  296. * :class:`django.views.generic.dates.BaseDayArchiveView`
  297. * :class:`django.views.generic.dates.YearMixin`
  298. * :class:`django.views.generic.dates.MonthMixin`
  299. * :class:`django.views.generic.dates.DayMixin`
  300. * :class:`django.views.generic.dates.BaseDateListView`
  301. * :class:`django.views.generic.list.MultipleObjectMixin`
  302. * :class:`django.views.generic.dates.DateMixin`
  303. * :class:`django.views.generic.base.View`
  304. **Context**
  305. In addition to the context provided by
  306. :class:`~django.views.generic.list.MultipleObjectMixin` (via
  307. :class:`~django.views.generic.dates.BaseDateListView`), the template's
  308. context will be:
  309. * ``day``: A :class:`~datetime.date` object
  310. representing the given day.
  311. * ``next_day``: A :class:`~datetime.date` object
  312. representing the next day, according to
  313. :attr:`~BaseDateListView.allow_empty` and
  314. :attr:`~DateMixin.allow_future`.
  315. * ``previous_day``: A :class:`~datetime.date` object
  316. representing the previous day, according to
  317. :attr:`~BaseDateListView.allow_empty` and
  318. :attr:`~DateMixin.allow_future`.
  319. * ``next_month``: A :class:`~datetime.date` object
  320. representing the first day of the next month, according to
  321. :attr:`~BaseDateListView.allow_empty` and
  322. :attr:`~DateMixin.allow_future`.
  323. * ``previous_month``: A :class:`~datetime.date` object
  324. representing the first day of the previous month, according to
  325. :attr:`~BaseDateListView.allow_empty` and
  326. :attr:`~DateMixin.allow_future`.
  327. **Notes**
  328. * Uses a default ``template_name_suffix`` of ``_archive_day``.
  329. **Example myapp/views.py**::
  330. from django.views.generic.dates import DayArchiveView
  331. from myapp.models import Article
  332. class ArticleDayArchiveView(DayArchiveView):
  333. queryset = Article.objects.all()
  334. date_field = "pub_date"
  335. allow_future = True
  336. **Example myapp/urls.py**::
  337. from django.conf.urls import url
  338. from myapp.views import ArticleDayArchiveView
  339. urlpatterns = [
  340. # Example: /2012/nov/10/
  341. url(r'^(?P<year>[0-9]{4})/(?P<month>[-\w]+)/(?P<day>[0-9]+)/$',
  342. ArticleDayArchiveView.as_view(),
  343. name="archive_day"),
  344. ]
  345. **Example myapp/article_archive_day.html**:
  346. .. code-block:: html+django
  347. <h1>{{ day }}</h1>
  348. <ul>
  349. {% for article in object_list %}
  350. <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
  351. {% endfor %}
  352. </ul>
  353. <p>
  354. {% if previous_day %}
  355. Previous Day: {{ previous_day }}
  356. {% endif %}
  357. {% if previous_day and next_day %}--{% endif %}
  358. {% if next_day %}
  359. Next Day: {{ next_day }}
  360. {% endif %}
  361. </p>
  362. TodayArchiveView
  363. ----------------
  364. .. class:: TodayArchiveView
  365. A day archive page showing all objects for *today*. This is exactly the
  366. same as :class:`django.views.generic.dates.DayArchiveView`, except today's
  367. date is used instead of the ``year``/``month``/``day`` arguments.
  368. **Ancestors (MRO)**
  369. * :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
  370. * :class:`django.views.generic.base.TemplateResponseMixin`
  371. * :class:`django.views.generic.dates.BaseTodayArchiveView`
  372. * :class:`django.views.generic.dates.BaseDayArchiveView`
  373. * :class:`django.views.generic.dates.YearMixin`
  374. * :class:`django.views.generic.dates.MonthMixin`
  375. * :class:`django.views.generic.dates.DayMixin`
  376. * :class:`django.views.generic.dates.BaseDateListView`
  377. * :class:`django.views.generic.list.MultipleObjectMixin`
  378. * :class:`django.views.generic.dates.DateMixin`
  379. * :class:`django.views.generic.base.View`
  380. **Notes**
  381. * Uses a default ``template_name_suffix`` of ``_archive_today``.
  382. **Example myapp/views.py**::
  383. from django.views.generic.dates import TodayArchiveView
  384. from myapp.models import Article
  385. class ArticleTodayArchiveView(TodayArchiveView):
  386. queryset = Article.objects.all()
  387. date_field = "pub_date"
  388. allow_future = True
  389. **Example myapp/urls.py**::
  390. from django.conf.urls import url
  391. from myapp.views import ArticleTodayArchiveView
  392. urlpatterns = [
  393. url(r'^today/$',
  394. ArticleTodayArchiveView.as_view(),
  395. name="archive_today"),
  396. ]
  397. .. admonition:: Where is the example template for ``TodayArchiveView``?
  398. This view uses by default the same template as the
  399. :class:`~DayArchiveView`, which is in the previous example. If you need
  400. a different template, set the ``template_name`` attribute to be the
  401. name of the new template.
  402. DateDetailView
  403. --------------
  404. .. class:: DateDetailView
  405. A page representing an individual object. If the object has a date value in
  406. the future, the view will throw a 404 error by default, unless you set
  407. ``allow_future`` to ``True``.
  408. **Ancestors (MRO)**
  409. * :class:`django.views.generic.detail.SingleObjectTemplateResponseMixin`
  410. * :class:`django.views.generic.base.TemplateResponseMixin`
  411. * :class:`django.views.generic.dates.BaseDateDetailView`
  412. * :class:`django.views.generic.dates.YearMixin`
  413. * :class:`django.views.generic.dates.MonthMixin`
  414. * :class:`django.views.generic.dates.DayMixin`
  415. * :class:`django.views.generic.dates.DateMixin`
  416. * ``django.views.generic.detail.BaseDetailView``
  417. * :class:`django.views.generic.detail.SingleObjectMixin`
  418. * :class:`django.views.generic.base.View`
  419. **Context**
  420. * Includes the single object associated with the ``model`` specified in
  421. the ``DateDetailView``.
  422. **Notes**
  423. * Uses a default ``template_name_suffix`` of ``_detail``.
  424. **Example myapp/urls.py**::
  425. from django.conf.urls import url
  426. from django.views.generic.dates import DateDetailView
  427. urlpatterns = [
  428. url(r'^(?P<year>[0-9]{4})/(?P<month>[-\w]+)/(?P<day>[0-9]+)/(?P<pk>[0-9]+)/$',
  429. DateDetailView.as_view(model=Article, date_field="pub_date"),
  430. name="archive_date_detail"),
  431. ]
  432. **Example myapp/article_detail.html**:
  433. .. code-block:: html+django
  434. <h1>{{ object.title }}</h1>
  435. .. note::
  436. All of the generic views listed above have matching ``Base`` views that
  437. only differ in that they do not include the
  438. :class:`~django.views.generic.list.MultipleObjectTemplateResponseMixin`
  439. (for the archive views) or
  440. :class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`
  441. (for the :class:`DateDetailView`):
  442. .. class:: BaseArchiveIndexView
  443. .. class:: BaseYearArchiveView
  444. .. class:: BaseMonthArchiveView
  445. .. class:: BaseWeekArchiveView
  446. .. class:: BaseDayArchiveView
  447. .. class:: BaseTodayArchiveView
  448. .. class:: BaseDateDetailView