generic-date-based.txt 21 KB

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