generic-date-based.txt 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  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. * ``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 patterns, url
  122. from myapp.views import ArticleYearArchiveView
  123. urlpatterns = patterns('',
  124. url(r'^(?P<year>\d{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. make_object_list = True
  189. allow_future = True
  190. **Example myapp/urls.py**::
  191. from django.conf.urls import patterns, url
  192. from myapp.views import ArticleMonthArchiveView
  193. urlpatterns = patterns('',
  194. # Example: /2012/aug/
  195. url(r'^(?P<year>\d{4})/(?P<month>[-\w]+)/$',
  196. ArticleMonthArchiveView.as_view(),
  197. name="archive_month"),
  198. # Example: /2012/08/
  199. url(r'^(?P<year>\d{4})/(?P<month>\d+)/$',
  200. ArticleMonthArchiveView.as_view(month_format='%m'),
  201. name="archive_month_numeric"),
  202. )
  203. **Example myapp/article_archive_month.html**:
  204. .. code-block:: html+django
  205. <ul>
  206. {% for article in object_list %}
  207. <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
  208. {% endfor %}
  209. </ul>
  210. <p>
  211. {% if previous_month %}
  212. Previous Month: {{ previous_month|date:"F Y" }}
  213. {% endif %}
  214. {% if next_month %}
  215. Next Month: {{ next_month|date:"F Y" }}
  216. {% endif %}
  217. </p>
  218. WeekArchiveView
  219. ---------------
  220. .. class:: WeekArchiveView
  221. A weekly archive page showing all objects in a given week. Objects with a
  222. date in the *future* are not displayed unless you set ``allow_future`` to
  223. ``True``.
  224. **Ancestors (MRO)**
  225. * :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
  226. * :class:`django.views.generic.base.TemplateResponseMixin`
  227. * :class:`django.views.generic.dates.BaseWeekArchiveView`
  228. * :class:`django.views.generic.dates.YearMixin`
  229. * :class:`django.views.generic.dates.WeekMixin`
  230. * :class:`django.views.generic.dates.BaseDateListView`
  231. * :class:`django.views.generic.list.MultipleObjectMixin`
  232. * :class:`django.views.generic.dates.DateMixin`
  233. * :class:`django.views.generic.base.View`
  234. **Context**
  235. In addition to the context provided by
  236. :class:`~django.views.generic.list.MultipleObjectMixin` (via
  237. :class:`~django.views.generic.dates.BaseDateListView`), the template's
  238. context will be:
  239. * ``week``: A :class:`~datetime.date` object
  240. representing the first day of the given week.
  241. * ``next_week``: A :class:`~datetime.date` object
  242. representing the first day of the next week, according to
  243. :attr:`~BaseDateListView.allow_empty` and
  244. :attr:`~DateMixin.allow_future`.
  245. * ``previous_week``: A :class:`~datetime.date` object
  246. representing the first day of the previous week, according to
  247. :attr:`~BaseDateListView.allow_empty` and
  248. :attr:`~DateMixin.allow_future`.
  249. **Notes**
  250. * Uses a default ``template_name_suffix`` of ``_archive_week``.
  251. **Example myapp/views.py**::
  252. from django.views.generic.dates import WeekArchiveView
  253. from myapp.models import Article
  254. class ArticleWeekArchiveView(WeekArchiveView):
  255. queryset = Article.objects.all()
  256. date_field = "pub_date"
  257. make_object_list = True
  258. week_format = "%W"
  259. allow_future = True
  260. **Example myapp/urls.py**::
  261. from django.conf.urls import patterns, url
  262. from myapp.views import ArticleWeekArchiveView
  263. urlpatterns = patterns('',
  264. # Example: /2012/week/23/
  265. url(r'^(?P<year>\d{4})/week/(?P<week>\d+)/$',
  266. ArticleWeekArchiveView.as_view(),
  267. name="archive_week"),
  268. )
  269. **Example myapp/article_archive_week.html**:
  270. .. code-block:: html+django
  271. <h1>Week {{ week|date:'W' }}</h1>
  272. <ul>
  273. {% for article in object_list %}
  274. <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
  275. {% endfor %}
  276. </ul>
  277. <p>
  278. {% if previous_week %}
  279. Previous Week: {{ previous_week|date:"F Y" }}
  280. {% endif %}
  281. {% if previous_week and next_week %}--{% endif %}
  282. {% if next_week %}
  283. Next week: {{ next_week|date:"F Y" }}
  284. {% endif %}
  285. </p>
  286. In this example, you are outputting the week number. The default
  287. ``week_format`` in the ``WeekArchiveView`` uses week format ``'%U'``
  288. which is based on the United States week system where the week begins on a
  289. Sunday. The ``'%W'`` format uses the ISO week format and its week
  290. begins on a Monday. The ``'%W'`` format is the same in both the
  291. :func:`~time.strftime` and the :tfilter:`date`.
  292. However, the :tfilter:`date` template filter does not have an equivalent
  293. output format that supports the US based week system. The :tfilter:`date`
  294. filter ``'%U'`` outputs the number of seconds since the Unix epoch.
  295. DayArchiveView
  296. --------------
  297. .. class:: DayArchiveView
  298. A day archive page showing all objects in a given day. Days in the future
  299. throw a 404 error, regardless of whether any objects exist for future days,
  300. unless you set ``allow_future`` to ``True``.
  301. **Ancestors (MRO)**
  302. * :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
  303. * :class:`django.views.generic.base.TemplateResponseMixin`
  304. * :class:`django.views.generic.dates.BaseDayArchiveView`
  305. * :class:`django.views.generic.dates.YearMixin`
  306. * :class:`django.views.generic.dates.MonthMixin`
  307. * :class:`django.views.generic.dates.DayMixin`
  308. * :class:`django.views.generic.dates.BaseDateListView`
  309. * :class:`django.views.generic.list.MultipleObjectMixin`
  310. * :class:`django.views.generic.dates.DateMixin`
  311. * :class:`django.views.generic.base.View`
  312. **Context**
  313. In addition to the context provided by
  314. :class:`~django.views.generic.list.MultipleObjectMixin` (via
  315. :class:`~django.views.generic.dates.BaseDateListView`), the template's
  316. context will be:
  317. * ``day``: A :class:`~datetime.date` object
  318. representing the given day.
  319. * ``next_day``: A :class:`~datetime.date` object
  320. representing the next day, according to
  321. :attr:`~BaseDateListView.allow_empty` and
  322. :attr:`~DateMixin.allow_future`.
  323. * ``previous_day``: A :class:`~datetime.date` object
  324. representing the previous day, according to
  325. :attr:`~BaseDateListView.allow_empty` and
  326. :attr:`~DateMixin.allow_future`.
  327. * ``next_month``: A :class:`~datetime.date` object
  328. representing the first day of the next month, according to
  329. :attr:`~BaseDateListView.allow_empty` and
  330. :attr:`~DateMixin.allow_future`.
  331. * ``previous_month``: A :class:`~datetime.date` object
  332. representing the first day of the previous month, according to
  333. :attr:`~BaseDateListView.allow_empty` and
  334. :attr:`~DateMixin.allow_future`.
  335. **Notes**
  336. * Uses a default ``template_name_suffix`` of ``_archive_day``.
  337. **Example myapp/views.py**::
  338. from django.views.generic.dates import DayArchiveView
  339. from myapp.models import Article
  340. class ArticleDayArchiveView(DayArchiveView):
  341. queryset = Article.objects.all()
  342. date_field = "pub_date"
  343. make_object_list = True
  344. allow_future = True
  345. **Example myapp/urls.py**::
  346. from django.conf.urls import patterns, url
  347. from myapp.views import ArticleDayArchiveView
  348. urlpatterns = patterns('',
  349. # Example: /2012/nov/10/
  350. url(r'^(?P<year>\d{4})/(?P<month>[-\w]+)/(?P<day>\d+)/$',
  351. ArticleDayArchiveView.as_view(),
  352. name="archive_day"),
  353. )
  354. **Example myapp/article_archive_day.html**:
  355. .. code-block:: html+django
  356. <h1>{{ day }}</h1>
  357. <ul>
  358. {% for article in object_list %}
  359. <li>{{ article.pub_date|date:"F j, Y" }}: {{ article.title }}</li>
  360. {% endfor %}
  361. </ul>
  362. <p>
  363. {% if previous_day %}
  364. Previous Day: {{ previous_day }}
  365. {% endif %}
  366. {% if previous_day and next_day %}--{% endif %}
  367. {% if next_day %}
  368. Next Day: {{ next_day }}
  369. {% endif %}
  370. </p>
  371. TodayArchiveView
  372. ----------------
  373. .. class:: TodayArchiveView
  374. A day archive page showing all objects for *today*. This is exactly the
  375. same as :class:`django.views.generic.dates.DayArchiveView`, except today's
  376. date is used instead of the ``year``/``month``/``day`` arguments.
  377. **Ancestors (MRO)**
  378. * :class:`django.views.generic.list.MultipleObjectTemplateResponseMixin`
  379. * :class:`django.views.generic.base.TemplateResponseMixin`
  380. * :class:`django.views.generic.dates.BaseTodayArchiveView`
  381. * :class:`django.views.generic.dates.BaseDayArchiveView`
  382. * :class:`django.views.generic.dates.YearMixin`
  383. * :class:`django.views.generic.dates.MonthMixin`
  384. * :class:`django.views.generic.dates.DayMixin`
  385. * :class:`django.views.generic.dates.BaseDateListView`
  386. * :class:`django.views.generic.list.MultipleObjectMixin`
  387. * :class:`django.views.generic.dates.DateMixin`
  388. * :class:`django.views.generic.base.View`
  389. **Notes**
  390. * Uses a default ``template_name_suffix`` of ``_archive_today``.
  391. **Example myapp/views.py**::
  392. from django.views.generic.dates import TodayArchiveView
  393. from myapp.models import Article
  394. class ArticleTodayArchiveView(TodayArchiveView):
  395. queryset = Article.objects.all()
  396. date_field = "pub_date"
  397. make_object_list = True
  398. allow_future = True
  399. **Example myapp/urls.py**::
  400. from django.conf.urls import patterns, url
  401. from myapp.views import ArticleTodayArchiveView
  402. urlpatterns = patterns('',
  403. url(r'^today/$',
  404. ArticleTodayArchiveView.as_view(),
  405. name="archive_today"),
  406. )
  407. .. admonition:: Where is the example template for ``TodayArchiveView``?
  408. This view uses by default the same template as the
  409. :class:`~DayArchiveView`, which is in the previous example. If you need
  410. a different template, set the ``template_name`` attribute to be the
  411. name of the new template.
  412. DateDetailView
  413. --------------
  414. .. class:: DateDetailView
  415. A page representing an individual object. If the object has a date value in
  416. the future, the view will throw a 404 error by default, unless you set
  417. ``allow_future`` to ``True``.
  418. **Ancestors (MRO)**
  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. * ``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 myapp/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 they do not include the
  448. :class:`~django.views.generic.list.MultipleObjectTemplateResponseMixin`
  449. (for the archive views) or
  450. :class:`~django.views.generic.detail.SingleObjectTemplateResponseMixin`
  451. (for the :class:`DateDetailView`):
  452. .. class:: BaseArchiveIndexView
  453. .. class:: BaseYearArchiveView
  454. .. class:: BaseMonthArchiveView
  455. .. class:: BaseWeekArchiveView
  456. .. class:: BaseDayArchiveView
  457. .. class:: BaseTodayArchiveView
  458. .. class:: BaseDateDetailView