generic-date-based.txt 20 KB

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