generic-date-based.txt 20 KB

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