| 1 |
============= |
|---|
| 2 |
Generic views |
|---|
| 3 |
============= |
|---|
| 4 |
|
|---|
| 5 |
Writing Web applications can be monotonous, because we repeat certain patterns |
|---|
| 6 |
again and again. In Django, the most common of these patterns have been |
|---|
| 7 |
abstracted into "generic views" that let you quickly provide common views of |
|---|
| 8 |
an object without actually needing to write any Python code. |
|---|
| 9 |
|
|---|
| 10 |
Django's generic views contain the following: |
|---|
| 11 |
|
|---|
| 12 |
* A set of views for doing list/detail interfaces (for example, |
|---|
| 13 |
Django's `documentation index`_ and `detail pages`_). |
|---|
| 14 |
|
|---|
| 15 |
* A set of views for year/month/day archive pages and associated |
|---|
| 16 |
detail and "latest" pages (for example, the Django weblog's year_, |
|---|
| 17 |
month_, day_, detail_, and latest_ pages). |
|---|
| 18 |
|
|---|
| 19 |
* A set of views for creating, editing, and deleting objects. |
|---|
| 20 |
|
|---|
| 21 |
.. _`documentation index`: http://www.djangoproject.com/documentation/ |
|---|
| 22 |
.. _`detail pages`: http://www.djangoproject.com/documentation/faq/ |
|---|
| 23 |
.. _year: http://www.djangoproject.com/weblog/2005/ |
|---|
| 24 |
.. _month: http://www.djangoproject.com/weblog/2005/jul/ |
|---|
| 25 |
.. _day: http://www.djangoproject.com/weblog/2005/jul/20/ |
|---|
| 26 |
.. _detail: http://www.djangoproject.com/weblog/2005/jul/20/autoreload/ |
|---|
| 27 |
.. _latest: http://www.djangoproject.com/weblog/ |
|---|
| 28 |
|
|---|
| 29 |
All of these views are used by creating configuration dictionaries in |
|---|
| 30 |
your URLconf files and passing those dictionaries as the third member of the |
|---|
| 31 |
URLconf tuple for a given pattern. For example, here's the URLconf for the |
|---|
| 32 |
simple weblog app that drives the blog on djangoproject.com:: |
|---|
| 33 |
|
|---|
| 34 |
from django.conf.urls.defaults import * |
|---|
| 35 |
from django_website.apps.blog.models import Entry |
|---|
| 36 |
|
|---|
| 37 |
info_dict = { |
|---|
| 38 |
'queryset': Entry.objects.all(), |
|---|
| 39 |
'date_field': 'pub_date', |
|---|
| 40 |
} |
|---|
| 41 |
|
|---|
| 42 |
urlpatterns = patterns('django.views.generic.date_based', |
|---|
| 43 |
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/(?P<slug>[-\w]+)/$', 'object_detail', dict(info_dict, slug_field='slug')), |
|---|
| 44 |
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/(?P<day>\w{1,2})/$', 'archive_day', info_dict), |
|---|
| 45 |
(r'^(?P<year>\d{4})/(?P<month>[a-z]{3})/$', 'archive_month', info_dict), |
|---|
| 46 |
(r'^(?P<year>\d{4})/$', 'archive_year', info_dict), |
|---|
| 47 |
(r'^$', 'archive_index', info_dict), |
|---|
| 48 |
) |
|---|
| 49 |
|
|---|
| 50 |
As you can see, this URLconf defines a few options in ``info_dict``. |
|---|
| 51 |
``'queryset'`` gives the generic view a ``QuerySet`` of objects to use (in this |
|---|
| 52 |
case, all of the ``Entry`` objects) and tells the generic view which model is |
|---|
| 53 |
being used. |
|---|
| 54 |
|
|---|
| 55 |
Documentation of each generic view follows, along with a list of all keyword |
|---|
| 56 |
arguments that a generic view expects. Remember that as in the example above, |
|---|
| 57 |
arguments may either come from the URL pattern (as ``month``, ``day``, |
|---|
| 58 |
``year``, etc. do above) or from the additional-information dictionary (as for |
|---|
| 59 |
``queryset``, ``date_field``, etc.). |
|---|
| 60 |
|
|---|
| 61 |
Most generic views require the ``queryset`` key, which is a ``QuerySet`` |
|---|
| 62 |
instance; see the `database API docs`_ for more information about ``Queryset`` |
|---|
| 63 |
objects. |
|---|
| 64 |
|
|---|
| 65 |
Most views also take an optional ``extra_context`` dictionary that you can use |
|---|
| 66 |
to pass any auxiliary information you wish to the view. The values in the |
|---|
| 67 |
``extra_context`` dictionary can be either functions (or other callables) or |
|---|
| 68 |
other objects. Functions are evaluated just before they are passed to the |
|---|
| 69 |
template. However, note that QuerySets retrieve and cache their data when they |
|---|
| 70 |
are first evaluated, so if you want to pass in a QuerySet via |
|---|
| 71 |
``extra_context`` that is always fresh you need to wrap it in a function or |
|---|
| 72 |
lambda that returns the QuerySet. |
|---|
| 73 |
|
|---|
| 74 |
.. _database API docs: ../db-api/ |
|---|
| 75 |
|
|---|
| 76 |
"Simple" generic views |
|---|
| 77 |
====================== |
|---|
| 78 |
|
|---|
| 79 |
The ``django.views.generic.simple`` module contains simple views to handle a |
|---|
| 80 |
couple of common cases: rendering a template when no view logic is needed, |
|---|
| 81 |
and issuing a redirect. |
|---|
| 82 |
|
|---|
| 83 |
``django.views.generic.simple.direct_to_template`` |
|---|
| 84 |
-------------------------------------------------- |
|---|
| 85 |
|
|---|
| 86 |
**Description:** |
|---|
| 87 |
|
|---|
| 88 |
Renders a given template, passing it a ``{{ params }}`` template variable, |
|---|
| 89 |
which is a dictionary of the parameters captured in the URL. |
|---|
| 90 |
|
|---|
| 91 |
**Required arguments:** |
|---|
| 92 |
|
|---|
| 93 |
* ``template``: The full name of a template to use. |
|---|
| 94 |
|
|---|
| 95 |
**Optional arguments:** |
|---|
| 96 |
|
|---|
| 97 |
* ``extra_context``: A dictionary of values to add to the template |
|---|
| 98 |
context. By default, this is an empty dictionary. If a value in the |
|---|
| 99 |
dictionary is callable, the generic view will call it |
|---|
| 100 |
just before rendering the template. |
|---|
| 101 |
|
|---|
| 102 |
* ``mimetype``: The MIME type to use for the resulting document. Defaults |
|---|
| 103 |
to the value of the ``DEFAULT_CONTENT_TYPE`` setting. |
|---|
| 104 |
|
|---|
| 105 |
**Example:** |
|---|
| 106 |
|
|---|
| 107 |
Given the following URL patterns:: |
|---|
| 108 |
|
|---|
| 109 |
urlpatterns = patterns('django.views.generic.simple', |
|---|
| 110 |
(r'^foo/$', 'direct_to_template', {'template': 'foo_index.html'}), |
|---|
| 111 |
(r'^foo/(?P<id>\d+)/$', 'direct_to_template', {'template': 'foo_detail.html'}), |
|---|
| 112 |
) |
|---|
| 113 |
|
|---|
| 114 |
... a request to ``/foo/`` would render the template ``foo_index.html``, and a |
|---|
| 115 |
request to ``/foo/15/`` would render the ``foo_detail.html`` with a context |
|---|
| 116 |
variable ``{{ params.id }}`` that is set to ``15``. |
|---|
| 117 |
|
|---|
| 118 |
``django.views.generic.simple.redirect_to`` |
|---|
| 119 |
------------------------------------------- |
|---|
| 120 |
|
|---|
| 121 |
**Description:** |
|---|
| 122 |
|
|---|
| 123 |
Redirects to a given URL. |
|---|
| 124 |
|
|---|
| 125 |
The given URL may contain dictionary-style string formatting, which will be |
|---|
| 126 |
interpolated against the parameters captured in the URL. |
|---|
| 127 |
|
|---|
| 128 |
If the given URL is ``None``, Django will return an ``HttpResponseGone`` (410). |
|---|
| 129 |
|
|---|
| 130 |
**Required arguments:** |
|---|
| 131 |
|
|---|
| 132 |
* ``url``: The URL to redirect to, as a string. Or ``None`` to raise a 410 |
|---|
| 133 |
(Gone) HTTP error. |
|---|
| 134 |
|
|---|
| 135 |
**Example:** |
|---|
| 136 |
|
|---|
| 137 |
This example redirects from ``/foo/<id>/`` to ``/bar/<id>/``:: |
|---|
| 138 |
|
|---|
| 139 |
urlpatterns = patterns('django.views.generic.simple', |
|---|
| 140 |
('^foo/(?P<id>\d+)/$', 'redirect_to', {'url': '/bar/%(id)s/'}), |
|---|
| 141 |
) |
|---|
| 142 |
|
|---|
| 143 |
This example returns a 410 HTTP error for requests to ``/bar/``:: |
|---|
| 144 |
|
|---|
| 145 |
urlpatterns = patterns('django.views.generic.simple', |
|---|
| 146 |
('^bar/$', 'redirect_to', {'url': None}), |
|---|
| 147 |
) |
|---|
| 148 |
|
|---|
| 149 |
Date-based generic views |
|---|
| 150 |
======================== |
|---|
| 151 |
|
|---|
| 152 |
Date-based generic views (in the module ``django.views.generic.date_based``) |
|---|
| 153 |
are views for displaying drilldown pages for date-based data. |
|---|
| 154 |
|
|---|
| 155 |
``django.views.generic.date_based.archive_index`` |
|---|
| 156 |
------------------------------------------------- |
|---|
| 157 |
|
|---|
| 158 |
**Description:** |
|---|
| 159 |
|
|---|
| 160 |
A top-level index page showing the "latest" objects, by date. Objects with |
|---|
| 161 |
a date in the *future* are not included unless you set ``allow_future`` to |
|---|
| 162 |
``True``. |
|---|
| 163 |
|
|---|
| 164 |
**Required arguments:** |
|---|
| 165 |
|
|---|
| 166 |
* ``queryset``: A ``QuerySet`` of objects for which the archive serves. |
|---|
| 167 |
|
|---|
| 168 |
* ``date_field``: The name of the ``DateField`` or ``DateTimeField`` in |
|---|
| 169 |
the ``QuerySet``'s model that the date-based archive should use to |
|---|
| 170 |
determine the objects on the page. |
|---|
| 171 |
|
|---|
| 172 |
**Optional arguments:** |
|---|
| 173 |
|
|---|
| 174 |
* ``num_latest``: The number of latest objects to send to the template |
|---|
| 175 |
context. By default, it's 15. |
|---|
| 176 |
|
|---|
| 177 |
* ``template_name``: The full name of a template to use in rendering the |
|---|
| 178 |
page. This lets you override the default template name (see below). |
|---|
| 179 |
|
|---|
| 180 |
* ``template_loader``: The template loader to use when loading the |
|---|
| 181 |
template. By default, it's ``django.template.loader``. |
|---|
| 182 |
|
|---|
| 183 |
* ``extra_context``: A dictionary of values to add to the template |
|---|
| 184 |
context. By default, this is an empty dictionary. If a value in the |
|---|
| 185 |
dictionary is callable, the generic view will call it |
|---|
| 186 |
just before rendering the template. |
|---|
| 187 |
|
|---|
| 188 |
* ``allow_empty``: A boolean specifying whether to display the page if no |
|---|
| 189 |
objects are available. If this is ``False`` and no objects are available, |
|---|
| 190 |
the view will raise a 404 instead of displaying an empty page. By |
|---|
| 191 |
default, this is ``False``. |
|---|
| 192 |
|
|---|
| 193 |
* ``context_processors``: A list of template-context processors to apply to |
|---|
| 194 |
the view's template. See the `RequestContext docs`_. |
|---|
| 195 |
|
|---|
| 196 |
* ``mimetype``: The MIME type to use for the resulting document. Defaults |
|---|
| 197 |
to the value of the ``DEFAULT_CONTENT_TYPE`` setting. |
|---|
| 198 |
|
|---|
| 199 |
* ``allow_future``: A boolean specifying whether to include "future" |
|---|
| 200 |
objects on this page, where "future" means objects in which the field |
|---|
| 201 |
specified in ``date_field`` is greater than the current date/time. By |
|---|
| 202 |
default, this is ``False``. |
|---|
| 203 |
|
|---|
| 204 |
**Template name:** |
|---|
| 205 |
|
|---|
| 206 |
If ``template_name`` isn't specified, this view will use the template |
|---|
| 207 |
``<app_label>/<model_name>_archive.html`` by default, where: |
|---|
| 208 |
|
|---|
| 209 |
* ``<model_name>`` is your model's name in all lowercase. For a model |
|---|
| 210 |
``StaffMember``, that'd be ``staffmember``. |
|---|
| 211 |
|
|---|
| 212 |
* ``<app_label>`` is the right-most part of the full Python path to |
|---|
| 213 |
your model's app. For example, if your model lives in |
|---|
| 214 |
``apps/blog/models.py``, that'd be ``blog``. |
|---|
| 215 |
|
|---|
| 216 |
**Template context:** |
|---|
| 217 |
|
|---|
| 218 |
In addition to ``extra_context``, the template's context will be: |
|---|
| 219 |
|
|---|
| 220 |
* ``date_list``: A list of ``datetime.date`` objects representing all |
|---|
| 221 |
years that have objects available according to ``queryset``. These are |
|---|
| 222 |
ordered in reverse. This is equivalent to |
|---|
| 223 |
``queryset.dates(date_field, 'year')[::-1]``. |
|---|
| 224 |
* ``latest``: The ``num_latest`` objects in the system, ordered descending |
|---|
| 225 |
by ``date_field``. For example, if ``num_latest`` is ``10``, then |
|---|
| 226 |
``latest`` will be a list of the latest 10 objects in ``queryset``. |
|---|
| 227 |
|
|---|
| 228 |
.. _RequestContext docs: ../templates_python/#subclassing-context-requestcontext |
|---|
| 229 |
|
|---|
| 230 |
``django.views.generic.date_based.archive_year`` |
|---|
| 231 |
------------------------------------------------ |
|---|
| 232 |
|
|---|
| 233 |
**Description:** |
|---|
| 234 |
|
|---|
| 235 |
A yearly archive page showing all available months in a given year. Objects |
|---|
| 236 |
with a date in the *future* are not displayed unless you set ``allow_future`` |
|---|
| 237 |
to ``True``. |
|---|
| 238 |
|
|---|
| 239 |
**Required arguments:** |
|---|
| 240 |
|
|---|
| 241 |
* ``year``: The four-digit year for which the archive serves. |
|---|
| 242 |
|
|---|
| 243 |
* ``queryset``: A ``QuerySet`` of objects for which the archive serves. |
|---|
| 244 |
|
|---|
| 245 |
* ``date_field``: The name of the ``DateField`` or ``DateTimeField`` in |
|---|
| 246 |
the ``QuerySet``'s model that the date-based archive should use to |
|---|
| 247 |
determine the objects on the page. |
|---|
| 248 |
|
|---|
| 249 |
**Optional arguments:** |
|---|
| 250 |
|
|---|
| 251 |
* ``template_name``: The full name of a template to use in rendering the |
|---|
| 252 |
page. This lets you override the default template name (see below). |
|---|
| 253 |
|
|---|
| 254 |
* ``template_loader``: The template loader to use when loading the |
|---|
| 255 |
template. By default, it's ``django.template.loader``. |
|---|
| 256 |
|
|---|
| 257 |
* ``extra_context``: A dictionary of values to add to the template |
|---|
| 258 |
context. By default, this is an empty dictionary. If a value in the |
|---|
| 259 |
dictionary is callable, the generic view will call it |
|---|
| 260 |
just before rendering the template. |
|---|
| 261 |
|
|---|
| 262 |
* ``allow_empty``: A boolean specifying whether to display the page if no |
|---|
| 263 |
objects are available. If this is ``False`` and no objects are available, |
|---|
| 264 |
the view will raise a 404 instead of displaying an empty page. By |
|---|
| 265 |
default, this is ``False``. |
|---|
| 266 |
|
|---|
| 267 |
* ``context_processors``: A list of template-context processors to apply to |
|---|
| 268 |
the view's template. See the `RequestContext docs`_. |
|---|
| 269 |
|
|---|
| 270 |
* ``template_object_name``: Designates the name of the template variable |
|---|
| 271 |
to use in the template context. By default, this is ``'object'``. The |
|---|
| 272 |
view will append ``'_list'`` to the value of this parameter in |
|---|
| 273 |
determining the variable's name. |
|---|
| 274 |
|
|---|
| 275 |
* ``make_object_list``: A boolean specifying whether to retrieve the full |
|---|
| 276 |
list of objects for this year and pass those to the template. If ``True``, |
|---|
| 277 |
this list of objects will be made available to the template as |
|---|
| 278 |
``object_list``. (The name ``object_list`` may be different; see the docs |
|---|
| 279 |
for ``object_list`` in the "Template context" section below.) By default, |
|---|
| 280 |
this is ``False``. |
|---|
| 281 |
|
|---|
| 282 |
* ``mimetype``: The MIME type to use for the resulting document. Defaults |
|---|
| 283 |
to the value of the ``DEFAULT_CONTENT_TYPE`` setting. |
|---|
| 284 |
|
|---|
| 285 |
* ``allow_future``: A boolean specifying whether to include "future" |
|---|
| 286 |
objects on this page, where "future" means objects in which the field |
|---|
| 287 |
specified in ``date_field`` is greater than the current date/time. By |
|---|
| 288 |
default, this is ``False``. |
|---|
| 289 |
|
|---|
| 290 |
**Template name:** |
|---|
| 291 |
|
|---|
| 292 |
If ``template_name`` isn't specified, this view will use the template |
|---|
| 293 |
``<app_label>/<model_name>_archive_year.html`` by default. |
|---|
| 294 |
|
|---|
| 295 |
**Template context:** |
|---|
| 296 |
|
|---|
| 297 |
In addition to ``extra_context``, the template's context will be: |
|---|
| 298 |
|
|---|
| 299 |
* ``date_list``: A list of ``datetime.date`` objects representing all |
|---|
| 300 |
months that have objects available in the given year, according to |
|---|
| 301 |
``queryset``, in ascending order. |
|---|
| 302 |
|
|---|
| 303 |
* ``year``: The given year, as a four-character string. |
|---|
| 304 |
|
|---|
| 305 |
* ``object_list``: If the ``make_object_list`` parameter is ``True``, this |
|---|
| 306 |
will be set to a list of objects available for the given year, ordered by |
|---|
| 307 |
the date field. This variable's name depends on the |
|---|
| 308 |
``template_object_name`` parameter, which is ``'object'`` by default. If |
|---|
| 309 |
``template_object_name`` is ``'foo'``, this variable's name will be |
|---|
| 310 |
``foo_list``. |
|---|
| 311 |
|
|---|
| 312 |
If ``make_object_list`` is ``False``, ``object_list`` will be passed to |
|---|
| 313 |
the template as an empty list. |
|---|
| 314 |
|
|---|
| 315 |
``django.views.generic.date_based.archive_month`` |
|---|
| 316 |
------------------------------------------------- |
|---|
| 317 |
|
|---|
| 318 |
**Description:** |
|---|
| 319 |
|
|---|
| 320 |
A monthly archive page showing all objects in a given month. Objects with a |
|---|
| 321 |
date in the *future* are not displayed unless you set ``allow_future`` to |
|---|
| 322 |
``True``. |
|---|
| 323 |
|
|---|
| 324 |
**Required arguments:** |
|---|
| 325 |
|
|---|
| 326 |
* ``year``: The four-digit year for which the archive serves (a string). |
|---|
| 327 |
|
|---|
| 328 |
* ``month``: The month for which the archive serves, formatted according to |
|---|
| 329 |
the ``month_format`` argument. |
|---|
| 330 |
|
|---|
| 331 |
* ``queryset``: A ``QuerySet`` of objects for which the archive serves. |
|---|
| 332 |
|
|---|
| 333 |
* ``date_field``: The name of the ``DateField`` or ``DateTimeField`` in |
|---|
| 334 |
the ``QuerySet``'s model that the date-based archive should use to |
|---|
| 335 |
determine the objects on the page. |
|---|
| 336 |
|
|---|
| 337 |
**Optional arguments:** |
|---|
| 338 |
|
|---|
| 339 |
* ``month_format``: A format string that regulates what format the |
|---|
| 340 |
``month`` parameter uses. This should be in the syntax accepted by |
|---|
| 341 |
Python's ``time.strftime``. (See the `strftime docs`_.) It's set to |
|---|
| 342 |
``"%b"`` by default, which is a three-letter month abbreviation. To |
|---|
| 343 |
change it to use numbers, use ``"%m"``. |
|---|
| 344 |
|
|---|
| 345 |
* ``template_name``: The full name of a template to use in rendering the |
|---|
| 346 |
page. This lets you override the default template name (see below). |
|---|
| 347 |
|
|---|
| 348 |
* ``template_loader``: The template loader to use when loading the |
|---|
| 349 |
template. By default, it's ``django.template.loader``. |
|---|
| 350 |
|
|---|
| 351 |
* ``extra_context``: A dictionary of values to add to the template |
|---|
| 352 |
context. By default, this is an empty dictionary. If a value in the |
|---|
| 353 |
dictionary is callable, the generic view will call it |
|---|
| 354 |
just before rendering the template. |
|---|
| 355 |
|
|---|
| 356 |
* ``allow_empty``: A boolean specifying whether to display the page if no |
|---|
| 357 |
objects are available. If this is ``False`` and no objects are available, |
|---|
| 358 |
the view will raise a 404 instead of displaying an empty page. By |
|---|
| 359 |
default, this is ``False``. |
|---|
| 360 |
|
|---|
| 361 |
* ``context_processors``: A list of template-context processors to apply to |
|---|
| 362 |
the view's template. See the `RequestContext docs`_. |
|---|
| 363 |
|
|---|
| 364 |
* ``template_object_name``: Designates the name of the template variable |
|---|
| 365 |
to use in the template context. By default, this is ``'object'``. The |
|---|
| 366 |
view will append ``'_list'`` to the value of this parameter in |
|---|
| 367 |
determining the variable's name. |
|---|
| 368 |
|
|---|
| 369 |
* ``mimetype``: The MIME type to use for the resulting document. Defaults |
|---|
| 370 |
to the value of the ``DEFAULT_CONTENT_TYPE`` setting. |
|---|
| 371 |
|
|---|
| 372 |
* ``allow_future``: A boolean specifying whether to include "future" |
|---|
| 373 |
objects on this page, where "future" means objects in which the field |
|---|
| 374 |
specified in ``date_field`` is greater than the current date/time. By |
|---|
| 375 |
default, this is ``False``. |
|---|
| 376 |
|
|---|
| 377 |
**Template name:** |
|---|
| 378 |
|
|---|
| 379 |
If ``template_name`` isn't specified, this view will use the template |
|---|
| 380 |
``<app_label>/<model_name>_archive_month.html`` by default. |
|---|
| 381 |
|
|---|
| 382 |
**Template context:** |
|---|
| 383 |
|
|---|
| 384 |
In addition to ``extra_context``, the template's context will be: |
|---|
| 385 |
|
|---|
| 386 |
* ``month``: A ``datetime.date`` object representing the given month. |
|---|
| 387 |
|
|---|
| 388 |
* ``next_month``: A ``datetime.date`` object representing the first day of |
|---|
| 389 |
the next month. If the next month is in the future, this will be |
|---|
| 390 |
``None``. |
|---|
| 391 |
|
|---|
| 392 |
* ``previous_month``: A ``datetime.date`` object representing the first day |
|---|
| 393 |
of the previous month. Unlike ``next_month``, this will never be |
|---|
| 394 |
``None``. |
|---|
| 395 |
|
|---|
| 396 |
* ``object_list``: A list of objects available for the given month. This |
|---|
| 397 |
variable's name depends on the ``template_object_name`` parameter, which |
|---|
| 398 |
is ``'object'`` by default. If ``template_object_name`` is ``'foo'``, |
|---|
| 399 |
this variable's name will be ``foo_list``. |
|---|
| 400 |
|
|---|
| 401 |
.. _strftime docs: http://www.python.org/doc/current/lib/module-time.html#l2h-1941 |
|---|
| 402 |
|
|---|
| 403 |
``django.views.generic.date_based.archive_week`` |
|---|
| 404 |
------------------------------------------------ |
|---|
| 405 |
|
|---|
| 406 |
**Description:** |
|---|
| 407 |
|
|---|
| 408 |
A weekly archive page showing all objects in a given week. Objects with a date |
|---|
| 409 |
in the *future* are not displayed unless you set ``allow_future`` to ``True``. |
|---|
| 410 |
|
|---|
| 411 |
**Required arguments:** |
|---|
| 412 |
|
|---|
| 413 |
* ``year``: The four-digit year for which the archive serves (a string). |
|---|
| 414 |
|
|---|
| 415 |
* ``week``: The week of the year for which the archive serves (a string). |
|---|
| 416 |
Weeks start with Sunday. |
|---|
| 417 |
|
|---|
| 418 |
* ``queryset``: A ``QuerySet`` of objects for which the archive serves. |
|---|
| 419 |
|
|---|
| 420 |
* ``date_field``: The name of the ``DateField`` or ``DateTimeField`` in |
|---|
| 421 |
the ``QuerySet``'s model that the date-based archive should use to |
|---|
| 422 |
determine the objects on the page. |
|---|
| 423 |
|
|---|
| 424 |
**Optional arguments:** |
|---|
| 425 |
|
|---|
| 426 |
* ``template_name``: The full name of a template to use in rendering the |
|---|
| 427 |
page. This lets you override the default template name (see below). |
|---|
| 428 |
|
|---|
| 429 |
* ``template_loader``: The template loader to use when loading the |
|---|
| 430 |
template. By default, it's ``django.template.loader``. |
|---|
| 431 |
|
|---|
| 432 |
* ``extra_context``: A dictionary of values to add to the template |
|---|
| 433 |
context. By default, this is an empty dictionary. If a value in the |
|---|
| 434 |
dictionary is callable, the generic view will call it |
|---|
| 435 |
just before rendering the template. |
|---|
| 436 |
|
|---|
| 437 |
* ``allow_empty``: A boolean specifying whether to display the page if no |
|---|
| 438 |
objects are available. If this is ``False`` and no objects are available, |
|---|
| 439 |
the view will raise a 404 instead of displaying an empty page. By |
|---|
| 440 |
default, this is ``True``. |
|---|
| 441 |
|
|---|
| 442 |
* ``context_processors``: A list of template-context processors to apply to |
|---|
| 443 |
the view's template. See the `RequestContext docs`_. |
|---|
| 444 |
|
|---|
| 445 |
* ``template_object_name``: Designates the name of the template variable |
|---|
| 446 |
to use in the template context. By default, this is ``'object'``. The |
|---|
| 447 |
view will append ``'_list'`` to the value of this parameter in |
|---|
| 448 |
determining the variable's name. |
|---|
| 449 |
|
|---|
| 450 |
* ``mimetype``: The MIME type to use for the resulting document. Defaults |
|---|
| 451 |
to the value of the ``DEFAULT_CONTENT_TYPE`` setting. |
|---|
| 452 |
|
|---|
| 453 |
* ``allow_future``: A boolean specifying whether to include "future" |
|---|
| 454 |
objects on this page, where "future" means objects in which the field |
|---|
| 455 |
specified in ``date_field`` is greater than the current date/time. By |
|---|
| 456 |
default, this is ``False``. |
|---|
| 457 |
|
|---|
| 458 |
**Template name:** |
|---|
| 459 |
|
|---|
| 460 |
If ``template_name`` isn't specified, this view will use the template |
|---|
| 461 |
``<app_label>/<model_name>_archive_week.html`` by default. |
|---|
| 462 |
|
|---|
| 463 |
**Template context:** |
|---|
| 464 |
|
|---|
| 465 |
In addition to ``extra_context``, the template's context will be: |
|---|
| 466 |
|
|---|
| 467 |
* ``week``: A ``datetime.date`` object representing the first day of the |
|---|
| 468 |
given week. |
|---|
| 469 |
|
|---|
| 470 |
* ``object_list``: A list of objects available for the given week. This |
|---|
| 471 |
variable's name depends on the ``template_object_name`` parameter, which |
|---|
| 472 |
is ``'object'`` by default. If ``template_object_name`` is ``'foo'``, |
|---|
| 473 |
this variable's name will be ``foo_list``. |
|---|
| 474 |
|
|---|
| 475 |
``django.views.generic.date_based.archive_day`` |
|---|
| 476 |
----------------------------------------------- |
|---|
| 477 |
|
|---|
| 478 |
**Description:** |
|---|
| 479 |
|
|---|
| 480 |
A day archive page showing all objects in a given day. Days in the future throw |
|---|
| 481 |
a 404 error, regardless of whether any objects exist for future days, unless |
|---|
| 482 |
you set ``allow_future`` to ``True``. |
|---|
| 483 |
|
|---|
| 484 |
**Required arguments:** |
|---|
| 485 |
|
|---|
| 486 |
* ``year``: The four-digit year for which the archive serves (a string). |
|---|
| 487 |
|
|---|
| 488 |
* ``month``: The month for which the archive serves, formatted according to |
|---|
| 489 |
the ``month_format`` argument. |
|---|
| 490 |
|
|---|
| 491 |
* ``day``: The day for which the archive serves, formatted according to the |
|---|
| 492 |
``day_format`` argument. |
|---|
| 493 |
|
|---|
| 494 |
* ``queryset``: A ``QuerySet`` of objects for which the archive serves. |
|---|
| 495 |
|
|---|
| 496 |
* ``date_field``: The name of the ``DateField`` or ``DateTimeField`` in |
|---|
| 497 |
the ``QuerySet``'s model that the date-based archive should use to |
|---|
| 498 |
determine the objects on the page. |
|---|
| 499 |
|
|---|
| 500 |
**Optional arguments:** |
|---|
| 501 |
|
|---|
| 502 |
* ``month_format``: A format string that regulates what format the |
|---|
| 503 |
``month`` parameter uses. This should be in the syntax accepted by |
|---|
| 504 |
Python's ``time.strftime``. (See the `strftime docs`_.) It's set to |
|---|
| 505 |
``"%b"`` by default, which is a three-letter month abbreviation. To |
|---|
| 506 |
change it to use numbers, use ``"%m"``. |
|---|
| 507 |
|
|---|
| 508 |
* ``day_format``: Like ``month_format``, but for the ``day`` parameter. |
|---|
| 509 |
It defaults to ``"%d"`` (day of the month as a decimal number, 01-31). |
|---|
| 510 |
|
|---|
| 511 |
* ``template_name``: The full name of a template to use in rendering the |
|---|
| 512 |
page. This lets you override the default template name (see below). |
|---|
| 513 |
|
|---|
| 514 |
* ``template_loader``: The template loader to use when loading the |
|---|
| 515 |
template. By default, it's ``django.template.loader``. |
|---|
| 516 |
|
|---|
| 517 |
* ``extra_context``: A dictionary of values to add to the template |
|---|
| 518 |
context. By default, this is an empty dictionary. If a value in the |
|---|
| 519 |
dictionary is callable, the generic view will call it |
|---|
| 520 |
just before rendering the template. |
|---|
| 521 |
|
|---|
| 522 |
* ``allow_empty``: A boolean specifying whether to display the page if no |
|---|
| 523 |
objects are available. If this is ``False`` and no objects are available, |
|---|
| 524 |
the view will raise a 404 instead of displaying an empty page. By |
|---|
| 525 |
default, this is ``False``. |
|---|
| 526 |
|
|---|
| 527 |
* ``context_processors``: A list of template-context processors to apply to |
|---|
| 528 |
the view's template. See the `RequestContext docs`_. |
|---|
| 529 |
|
|---|
| 530 |
* ``template_object_name``: Designates the name of the template variable |
|---|
| 531 |
to use in the template context. By default, this is ``'object'``. The |
|---|
| 532 |
view will append ``'_list'`` to the value of this parameter in |
|---|
| 533 |
determining the variable's name. |
|---|
| 534 |
|
|---|
| 535 |
* ``mimetype``: The MIME type to use for the resulting document. Defaults |
|---|
| 536 |
to the value of the ``DEFAULT_CONTENT_TYPE`` setting. |
|---|
| 537 |
|
|---|
| 538 |
* ``allow_future``: A boolean specifying whether to include "future" |
|---|
| 539 |
objects on this page, where "future" means objects in which the field |
|---|
| 540 |
specified in ``date_field`` is greater than the current date/time. By |
|---|
| 541 |
default, this is ``False``. |
|---|
| 542 |
|
|---|
| 543 |
**Template name:** |
|---|
| 544 |
|
|---|
| 545 |
If ``template_name`` isn't specified, this view will use the template |
|---|
| 546 |
``<app_label>/<model_name>_archive_day.html`` by default. |
|---|
| 547 |
|
|---|
| 548 |
**Template context:** |
|---|
| 549 |
|
|---|
| 550 |
In addition to ``extra_context``, the template's context will be: |
|---|
| 551 |
|
|---|
| 552 |
* ``day``: A ``datetime.date`` object representing the given day. |
|---|
| 553 |
|
|---|
| 554 |
* ``next_day``: A ``datetime.date`` object representing the next day. If |
|---|
| 555 |
the next day is in the future, this will be ``None``. |
|---|
| 556 |
|
|---|
| 557 |
* ``previous_day``: A ``datetime.date`` object representing the given day. |
|---|
| 558 |
Unlike ``next_day``, this will never be ``None``. |
|---|
| 559 |
|
|---|
| 560 |
* ``object_list``: A list of objects available for the given day. This |
|---|
| 561 |
variable's name depends on the ``template_object_name`` parameter, which |
|---|
| 562 |
is ``'object'`` by default. If ``template_object_name`` is ``'foo'``, |
|---|
| 563 |
this variable's name will be ``foo_list``. |
|---|
| 564 |
|
|---|
| 565 |
``django.views.generic.date_based.archive_today`` |
|---|
| 566 |
------------------------------------------------- |
|---|
| 567 |
|
|---|
| 568 |
**Description:** |
|---|
| 569 |
|
|---|
| 570 |
A day archive page showing all objects for *today*. This is exactly the same as |
|---|
| 571 |
``archive_day``, except the ``year``/``month``/``day`` arguments are not used, |
|---|
| 572 |
and today's date is used instead. |
|---|
| 573 |
|
|---|
| 574 |
``django.views.generic.date_based.object_detail`` |
|---|
| 575 |
------------------------------------------------- |
|---|
| 576 |
|
|---|
| 577 |
**Description:** |
|---|
| 578 |
|
|---|
| 579 |
A page representing an individual object. If the object has a date value in the |
|---|
| 580 |
future, the view will throw a 404 error by default, unless you set |
|---|
| 581 |
``allow_future`` to ``True``. |
|---|
| 582 |
|
|---|
| 583 |
**Required arguments:** |
|---|
| 584 |
|
|---|
| 585 |
* ``year``: The object's four-digit year (a string). |
|---|
| 586 |
|
|---|
| 587 |
* ``month``: The object's month , formatted according to the |
|---|
| 588 |
``month_format`` argument. |
|---|
| 589 |
|
|---|
| 590 |
* ``day``: The object's day , formatted according to the ``day_format`` |
|---|
| 591 |
argument. |
|---|
| 592 |
|
|---|
| 593 |
* ``queryset``: A ``QuerySet`` that contains the object. |
|---|
| 594 |
|
|---|
| 595 |
* ``date_field``: The name of the ``DateField`` or ``DateTimeField`` in |
|---|
| 596 |
the ``QuerySet``'s model that the generic view should use to look up the |
|---|
| 597 |
object according to ``year``, ``month`` and ``day``. |
|---|
| 598 |
|
|---|
| 599 |
* Either ``object_id`` or (``slug`` *and* ``slug_field``) is required. |
|---|
| 600 |
|
|---|
| 601 |
If you provide ``object_id``, it should be the value of the primary-key |
|---|
| 602 |
field for the object being displayed on this page. |
|---|
| 603 |
|
|---|
| 604 |
Otherwise, ``slug`` should be the slug of the given object, and |
|---|
| 605 |
``slug_field`` should be the name of the slug field in the ``QuerySet``'s |
|---|
| 606 |
model. |
|---|
| 607 |
|
|---|
| 608 |
**Optional arguments:** |
|---|
| 609 |
|
|---|
| 610 |
* ``month_format``: A format string that regulates what format the |
|---|
| 611 |
``month`` parameter uses. This should be in the syntax accepted by |
|---|
| 612 |
Python's ``time.strftime``. (See the `strftime docs`_.) It's set to |
|---|
| 613 |
``"%b"`` by default, which is a three-letter month abbreviation. To |
|---|
| 614 |
change it to use numbers, use ``"%m"``. |
|---|
| 615 |
|
|---|
| 616 |
* ``day_format``: Like ``month_format``, but for the ``day`` parameter. |
|---|
| 617 |
It defaults to ``"%d"`` (day of the month as a decimal number, 01-31). |
|---|
| 618 |
|
|---|
| 619 |
* ``template_name``: The full name of a template to use in rendering the |
|---|
| 620 |
page. This lets you override the default template name (see below). |
|---|
| 621 |
|
|---|
| 622 |
* ``template_name_field``: The name of a field on the object whose value is |
|---|
| 623 |
the template name to use. This lets you store template names in the data. |
|---|
| 624 |
In other words, if your object has a field ``'the_template'`` that |
|---|
| 625 |
contains a string ``'foo.html'``, and you set ``template_name_field`` to |
|---|
| 626 |
``'the_template'``, then the generic view for this object will use the |
|---|
| 627 |
template ``'foo.html'``. |
|---|
| 628 |
|
|---|
| 629 |
It's a bit of a brain-bender, but it's useful in some cases. |
|---|
| 630 |
|
|---|
| 631 |
* ``template_loader``: The template loader to use when loading the |
|---|
| 632 |
template. By default, it's ``django.template.loader``. |
|---|
| 633 |
|
|---|
| 634 |
* ``extra_context``: A dictionary of values to add to the template |
|---|
| 635 |
context. By default, this is an empty dictionary. If a value in the |
|---|
| 636 |
dictionary is callable, the generic view will call it |
|---|
| 637 |
just before rendering the template. |
|---|
| 638 |
|
|---|
| 639 |
* ``context_processors``: A list of template-context processors to apply to |
|---|
| 640 |
the view's template. See the `RequestContext docs`_. |
|---|
| 641 |
|
|---|
| 642 |
* ``template_object_name``: Designates the name of the template variable |
|---|
| 643 |
to use in the template context. By default, this is ``'object'``. |
|---|
| 644 |
|
|---|
| 645 |
* ``mimetype``: The MIME type to use for the resulting document. Defaults |
|---|
| 646 |
to the value of the ``DEFAULT_CONTENT_TYPE`` setting. |
|---|
| 647 |
|
|---|
| 648 |
* ``allow_future``: A boolean specifying whether to include "future" |
|---|
| 649 |
objects on this page, where "future" means objects in which the field |
|---|
| 650 |
specified in ``date_field`` is greater than the current date/time. By |
|---|
| 651 |
default, this is ``False``. |
|---|
| 652 |
|
|---|
| 653 |
**Template name:** |
|---|
| 654 |
|
|---|
| 655 |
If ``template_name`` isn't specified, this view will use the template |
|---|
| 656 |
``<app_label>/<model_name>_detail.html`` by default. |
|---|
| 657 |
|
|---|
| 658 |
**Template context:** |
|---|
| 659 |
|
|---|
| 660 |
In addition to ``extra_context``, the template's context will be: |
|---|
| 661 |
|
|---|
| 662 |
* ``object``: The object. This variable's name depends on the |
|---|
| 663 |
``template_object_name`` parameter, which is ``'object'`` by default. If |
|---|
| 664 |
``template_object_name`` is ``'foo'``, this variable's name will be |
|---|
| 665 |
``foo``. |
|---|
| 666 |
|
|---|
| 667 |
List/detail generic views |
|---|
| 668 |
========================= |
|---|
| 669 |
|
|---|
| 670 |
The list-detail generic-view framework (in the |
|---|
| 671 |
``django.views.generic.list_detail`` module) is similar to the date-based one, |
|---|
| 672 |
except the former simply has two views: a list of objects and an individual |
|---|
| 673 |
object page. |
|---|
| 674 |
|
|---|
| 675 |
``django.views.generic.list_detail.object_list`` |
|---|
| 676 |
------------------------------------------------ |
|---|
| 677 |
|
|---|
| 678 |
**Description:** |
|---|
| 679 |
|
|---|
| 680 |
A page representing a list of objects. |
|---|
| 681 |
|
|---|
| 682 |
**Required arguments:** |
|---|
| 683 |
|
|---|
| 684 |
* ``queryset``: A ``QuerySet`` that represents the objects. |
|---|
| 685 |
|
|---|
| 686 |
**Optional arguments:** |
|---|
| 687 |
|
|---|
| 688 |
* ``paginate_by``: An integer specifying how many objects should be |
|---|
| 689 |
displayed per page. If this is given, the view will paginate objects with |
|---|
| 690 |
``paginate_by`` objects per page. The view will expect either a ``page`` |
|---|
| 691 |
query string parameter (via ``GET``) containing a 1-based page |
|---|
| 692 |
number, or a ``page`` variable specified in the URLconf. See |
|---|
| 693 |
"Notes on pagination" below. |
|---|
| 694 |
|
|---|
| 695 |
* ``template_name``: The full name of a template to use in rendering the |
|---|
| 696 |
page. This lets you override the default template name (see below). |
|---|
| 697 |
|
|---|
| 698 |
* ``template_loader``: The template loader to use when loading the |
|---|
| 699 |
template. By default, it's ``django.template.loader``. |
|---|
| 700 |
|
|---|
| 701 |
* ``extra_context``: A dictionary of values to add to the template |
|---|
| 702 |
context. By default, this is an empty dictionary. If a value in the |
|---|
| 703 |
dictionary is callable, the generic view will call it |
|---|
| 704 |
just before rendering the template. |
|---|
| 705 |
|
|---|
| 706 |
* ``allow_empty``: A boolean specifying whether to display the page if no |
|---|
| 707 |
objects are available. If this is ``False`` and no objects are available, |
|---|
| 708 |
the view will raise a 404 instead of displaying an empty page. By |
|---|
| 709 |
default, this is ``False``. |
|---|
| 710 |
|
|---|
| 711 |
* ``context_processors``: A list of template-context processors to apply to |
|---|
| 712 |
the view's template. See the `RequestContext docs`_. |
|---|
| 713 |
|
|---|
| 714 |
* ``template_object_name``: Designates the name of the template variable |
|---|
| 715 |
to use in the template context. By default, this is ``'object'``. The |
|---|
| 716 |
view will append ``'_list'`` to the value of this parameter in |
|---|
| 717 |
determining the variable's name. |
|---|
| 718 |
|
|---|
| 719 |
* ``mimetype``: The MIME type to use for the resulting document. Defaults |
|---|
| 720 |
to the value of the ``DEFAULT_CONTENT_TYPE`` setting. |
|---|
| 721 |
|
|---|
| 722 |
**Template name:** |
|---|
| 723 |
|
|---|
| 724 |
If ``template_name`` isn't specified, this view will use the template |
|---|
| 725 |
``<app_label>/<model_name>_list.html`` by default. |
|---|
| 726 |
|
|---|
| 727 |
**Template context:** |
|---|
| 728 |
|
|---|
| 729 |
In addition to ``extra_context``, the template's context will be: |
|---|
| 730 |
|
|---|
| 731 |
* ``object_list``: The list of objects. This variable's name depends on the |
|---|
| 732 |
``template_object_name`` parameter, which is ``'object'`` by default. If |
|---|
| 733 |
``template_object_name`` is ``'foo'``, this variable's name will be |
|---|
| 734 |
``foo_list``. |
|---|
| 735 |
|
|---|
| 736 |
* ``is_paginated``: A boolean representing whether the results are |
|---|
| 737 |
paginated. Specifically, this is set to ``False`` if the number of |
|---|
| 738 |
available objects is less than or equal to ``paginate_by``. |
|---|
| 739 |
|
|---|
| 740 |
If the results are paginated, the context will contain these extra variables: |
|---|
| 741 |
|
|---|
| 742 |
* ``results_per_page``: The number of objects per page. (Same as the |
|---|
| 743 |
``paginate_by`` parameter.) |
|---|
| 744 |
|
|---|
| 745 |
* ``has_next``: A boolean representing whether there's a next page. |
|---|
| 746 |
|
|---|
| 747 |
* ``has_previous``: A boolean representing whether there's a previous page. |
|---|
| 748 |
|
|---|
| 749 |
* ``page``: The current page number, as an integer. This is 1-based. |
|---|
| 750 |
|
|---|
| 751 |
* ``next``: The next page number, as an integer. If there's no next page, |
|---|
| 752 |
this will still be an integer representing the theoretical next-page |
|---|
| 753 |
number. This is 1-based. |
|---|
| 754 |
|
|---|
| 755 |
* ``previous``: The previous page number, as an integer. This is 1-based. |
|---|
| 756 |
|
|---|
| 757 |
* ``last_on_page``: The number of the |
|---|
| 758 |
last result on the current page. This is 1-based. |
|---|
| 759 |
|
|---|
| 760 |
* ``first_on_page``: The number of the |
|---|
| 761 |
first result on the current page. This is 1-based. |
|---|
| 762 |
|
|---|
| 763 |
* ``pages``: The total number of pages, as an integer. |
|---|
| 764 |
|
|---|
| 765 |
* ``hits``: The total number of objects across *all* pages, not just this |
|---|
| 766 |
page. |
|---|
| 767 |
|
|---|
| 768 |
Notes on pagination |
|---|
| 769 |
~~~~~~~~~~~~~~~~~~~ |
|---|
| 770 |
|
|---|
| 771 |
If ``paginate_by`` is specified, Django will paginate the results. You can |
|---|
| 772 |
specify the page number in the URL in one of two ways: |
|---|
| 773 |
|
|---|
| 774 |
* Use the ``page`` parameter in the URLconf. For example, this is what |
|---|
| 775 |
your URLconf might look like:: |
|---|
| 776 |
|
|---|
| 777 |
(r'^objects/page(?P<page>[0-9]+)/$', 'object_list', dict(info_dict)) |
|---|
| 778 |
|
|---|
| 779 |
* Pass the page number via the ``page`` query-string parameter. For |
|---|
| 780 |
example, a URL would look like this: |
|---|
| 781 |
|
|---|
| 782 |
/objects/?page=3 |
|---|
| 783 |
|
|---|
| 784 |
In both cases, ``page`` is 1-based, not 0-based, so the first page would be |
|---|
| 785 |
represented as page ``1``. |
|---|
| 786 |
|
|---|
| 787 |
``django.views.generic.list_detail.object_detail`` |
|---|
| 788 |
-------------------------------------------------- |
|---|
| 789 |
|
|---|
| 790 |
A page representing an individual object. |
|---|
| 791 |
|
|---|
| 792 |
**Description:** |
|---|
| 793 |
|
|---|
| 794 |
A page representing an individual object. |
|---|
| 795 |
|
|---|
| 796 |
**Required arguments:** |
|---|
| 797 |
|
|---|
| 798 |
* ``queryset``: A ``QuerySet`` that contains the object. |
|---|
| 799 |
|
|---|
| 800 |
* Either ``object_id`` or (``slug`` *and* ``slug_field``) is required. |
|---|
| 801 |
|
|---|
| 802 |
If you provide ``object_id``, it should be the value of the primary-key |
|---|
| 803 |
field for the object being displayed on this page. |
|---|
| 804 |
|
|---|
| 805 |
Otherwise, ``slug`` should be the slug of the given object, and |
|---|
| 806 |
``slug_field`` should be the name of the slug field in the ``QuerySet``'s |
|---|
| 807 |
model. |
|---|
| 808 |
|
|---|
| 809 |
**Optional arguments:** |
|---|
| 810 |
|
|---|
| 811 |
* ``template_name``: The full name of a template to use in rendering the |
|---|
| 812 |
page. This lets you override the default template name (see below). |
|---|
| 813 |
|
|---|
| 814 |
* ``template_name_field``: The name of a field on the object whose value is |
|---|
| 815 |
the template name to use. This lets you store template names in the data. |
|---|
| 816 |
In other words, if your object has a field ``'the_template'`` that |
|---|
| 817 |
contains a string ``'foo.html'``, and you set ``template_name_field`` to |
|---|
| 818 |
``'the_template'``, then the generic view for this object will use the |
|---|
| 819 |
template ``'foo.html'``. |
|---|
| 820 |
|
|---|
| 821 |
It's a bit of a brain-bender, but it's useful in some cases. |
|---|
| 822 |
|
|---|
| 823 |
* ``template_loader``: The template loader to use when loading the |
|---|
| 824 |
template. By default, it's ``django.template.loader``. |
|---|
| 825 |
|
|---|
| 826 |
* ``extra_context``: A dictionary of values to add to the template |
|---|
| 827 |
context. By default, this is an empty dictionary. If a value in the |
|---|
| 828 |
dictionary is callable, the generic view will call it |
|---|
| 829 |
just before rendering the template. |
|---|
| 830 |
|
|---|
| 831 |
* ``context_processors``: A list of template-context processors to apply to |
|---|
| 832 |
the view's template. See the `RequestContext docs`_. |
|---|
| 833 |
|
|---|
| 834 |
* ``template_object_name``: Designates the name of the template variable |
|---|
| 835 |
to use in the template context. By default, this is ``'object'``. |
|---|
| 836 |
|
|---|
| 837 |
* ``mimetype``: The MIME type to use for the resulting document. Defaults |
|---|
| 838 |
to the value of the ``DEFAULT_CONTENT_TYPE`` setting. |
|---|
| 839 |
|
|---|
| 840 |
**Template name:** |
|---|
| 841 |
|
|---|
| 842 |
If ``template_name`` isn't specified, this view will use the template |
|---|
| 843 |
``<app_label>/<model_name>_detail.html`` by default. |
|---|
| 844 |
|
|---|
| 845 |
**Template context:** |
|---|
| 846 |
|
|---|
| 847 |
In addition to ``extra_context``, the template's context will be: |
|---|
| 848 |
|
|---|
| 849 |
* ``object``: The object. This variable's name depends on the |
|---|
| 850 |
``template_object_name`` parameter, which is ``'object'`` by default. If |
|---|
| 851 |
``template_object_name`` is ``'foo'``, this variable's name will be |
|---|
| 852 |
``foo``. |
|---|
| 853 |
|
|---|
| 854 |
Create/update/delete generic views |
|---|
| 855 |
================================== |
|---|
| 856 |
|
|---|
| 857 |
The ``django.views.generic.create_update`` module contains a set of functions |
|---|
| 858 |
for creating, editing and deleting objects. |
|---|
| 859 |
|
|---|
| 860 |
``django.views.generic.create_update.create_object`` |
|---|
| 861 |
---------------------------------------------------- |
|---|
| 862 |
|
|---|
| 863 |
**Description:** |
|---|
| 864 |
|
|---|
| 865 |
A page that displays a form for creating an object, redisplaying the form with |
|---|
| 866 |
validation errors (if there are any) and saving the object. This uses the |
|---|
| 867 |
automatic manipulators that come with Django models. |
|---|
| 868 |
|
|---|
| 869 |
**Required arguments:** |
|---|
| 870 |
|
|---|
| 871 |
* ``model``: The Django model class of the object that the form will |
|---|
| 872 |
create. |
|---|
| 873 |
|
|---|
| 874 |
**Optional arguments:** |
|---|
| 875 |
|
|---|
| 876 |
* ``post_save_redirect``: A URL to which the view will redirect after |
|---|
| 877 |
saving the object. By default, it's ``object.get_absolute_url()``. |
|---|
| 878 |
|
|---|
| 879 |
``post_save_redirect`` may contain dictionary string formatting, which |
|---|
| 880 |
will be interpolated against the object's field attributes. For example, |
|---|
| 881 |
you could use ``post_save_redirect="/polls/%(slug)s/"``. |
|---|
| 882 |
|
|---|
| 883 |
* ``login_required``: A boolean that designates whether a user must be |
|---|
| 884 |
logged in, in order to see the page and save changes. This hooks into the |
|---|
| 885 |
Django `authentication system`_. By default, this is ``False``. |
|---|
| 886 |
|
|---|
| 887 |
If this is ``True``, and a non-logged-in user attempts to visit this page |
|---|
| 888 |
or save the form, Django will redirect the request to ``/accounts/login/``. |
|---|
| 889 |
|
|---|
| 890 |
* ``template_name``: The full name of a template to use in rendering the |
|---|
| 891 |
page. This lets you override the default template name (see below). |
|---|
| 892 |
|
|---|
| 893 |
* ``template_loader``: The template loader to use when loading the |
|---|
| 894 |
template. By default, it's ``django.template.loader``. |
|---|
| 895 |
|
|---|
| 896 |
* ``extra_context``: A dictionary of values to add to the template |
|---|
| 897 |
context. By default, this is an empty dictionary. If a value in the |
|---|
| 898 |
dictionary is callable, the generic view will call it |
|---|
| 899 |
just before rendering the template. |
|---|
| 900 |
|
|---|
| 901 |
* ``context_processors``: A list of template-context processors to apply to |
|---|
| 902 |
the view's template. See the `RequestContext docs`_. |
|---|
| 903 |
|
|---|
| 904 |
**Template name:** |
|---|
| 905 |
|
|---|
| 906 |
If ``template_name`` isn't specified, this view will use the template |
|---|
| 907 |
``<app_label>/<model_name>_form.html`` by default. |
|---|
| 908 |
|
|---|
| 909 |
**Template context:** |
|---|
| 910 |
|
|---|
| 911 |
In addition to ``extra_context``, the template's context will be: |
|---|
| 912 |
|
|---|
| 913 |
* ``form``: A ``django.oldforms.FormWrapper`` instance representing the form |
|---|
| 914 |
for editing the object. This lets you refer to form fields easily in the |
|---|
| 915 |
template system. |
|---|
| 916 |
|
|---|
| 917 |
For example, if ``model`` has two fields, ``name`` and ``address``:: |
|---|
| 918 |
|
|---|
| 919 |
<form action="" method="post"> |
|---|
| 920 |
<p><label for="id_name">Name:</label> {{ form.name }}</p> |
|---|
| 921 |
<p><label for="id_address">Address:</label> {{ form.address }}</p> |
|---|
| 922 |
</form> |
|---|
| 923 |
|
|---|
| 924 |
See the `manipulator and formfield documentation`_ for more information |
|---|
| 925 |
about using ``FormWrapper`` objects in templates. |
|---|
| 926 |
|
|---|
| 927 |
.. _authentication system: ../authentication/ |
|---|
| 928 |
.. _manipulator and formfield documentation: ../forms/ |
|---|
| 929 |
|
|---|
| 930 |
``django.views.generic.create_update.update_object`` |
|---|
| 931 |
---------------------------------------------------- |
|---|
| 932 |
|
|---|
| 933 |
**Description:** |
|---|
| 934 |
|
|---|
| 935 |
A page that displays a form for editing an existing object, redisplaying the |
|---|
| 936 |
form with validation errors (if there are any) and saving changes to the |
|---|
| 937 |
object. This uses the automatic manipulators that come with Django models. |
|---|
| 938 |
|
|---|
| 939 |
**Required arguments:** |
|---|
| 940 |
|
|---|
| 941 |
* ``model``: The Django model class of the object that the form will |
|---|
| 942 |
create. |
|---|
| 943 |
|
|---|
| 944 |
* Either ``object_id`` or (``slug`` *and* ``slug_field``) is required. |
|---|
| 945 |
|
|---|
| 946 |
If you provide ``object_id``, it should be the value of the primary-key |
|---|
| 947 |
field for the object being displayed on this page. |
|---|
| 948 |
|
|---|
| 949 |
Otherwise, ``slug`` should be the slug of the given object, and |
|---|
| 950 |
``slug_field`` should be the name of the slug field in the ``QuerySet``'s |
|---|
| 951 |
model. |
|---|
| 952 |
|
|---|
| 953 |
**Optional arguments:** |
|---|
| 954 |
|
|---|
| 955 |
* ``post_save_redirect``: A URL to which the view will redirect after |
|---|
| 956 |
saving the object. By default, it's ``object.get_absolute_url()``. |
|---|
| 957 |
|
|---|
| 958 |
``post_save_redirect`` may contain dictionary string formatting, which |
|---|
| 959 |
will be interpolated against the object's field attributes. For example, |
|---|
| 960 |
you could use ``post_save_redirect="/polls/%(slug)s/"``. |
|---|
| 961 |
|
|---|
| 962 |
* ``login_required``: A boolean that designates whether a user must be |
|---|
| 963 |
logged in, in order to see the page and save changes. This hooks into the |
|---|
| 964 |
Django `authentication system`_. By default, this is ``False``. |
|---|
| 965 |
|
|---|
| 966 |
If this is ``True``, and a non-logged-in user attempts to visit this page |
|---|
| 967 |
or save the form, Django will redirect the request to ``/accounts/login/``. |
|---|
| 968 |
|
|---|
| 969 |
* ``template_name``: The full name of a template to use in rendering the |
|---|
| 970 |
page. This lets you override the default template name (see below). |
|---|
| 971 |
|
|---|
| 972 |
* ``template_loader``: The template loader to use when loading the |
|---|
| 973 |
template. By default, it's ``django.template.loader``. |
|---|
| 974 |
|
|---|
| 975 |
* ``extra_context``: A dictionary of values to add to the template |
|---|
| 976 |
context. By default, this is an empty dictionary. If a value in the |
|---|
| 977 |
dictionary is callable, the generic view will call it |
|---|
| 978 |
just before rendering the template. |
|---|
| 979 |
|
|---|
| 980 |
* ``context_processors``: A list of template-context processors to apply to |
|---|
| 981 |
the view's template. See the `RequestContext docs`_. |
|---|
| 982 |
|
|---|
| 983 |
* ``template_object_name``: Designates the name of the template variable |
|---|
| 984 |
to use in the template context. By default, this is ``'object'``. |
|---|
| 985 |
|
|---|
| 986 |
**Template name:** |
|---|
| 987 |
|
|---|
| 988 |
If ``template_name`` isn't specified, this view will use the template |
|---|
| 989 |
``<app_label>/<model_name>_form.html`` by default. |
|---|
| 990 |
|
|---|
| 991 |
**Template context:** |
|---|
| 992 |
|
|---|
| 993 |
In addition to ``extra_context``, the template's context will be: |
|---|
| 994 |
|
|---|
| 995 |
* ``form``: A ``django.oldforms.FormWrapper`` instance representing the form |
|---|
| 996 |
for editing the object. This lets you refer to form fields easily in the |
|---|
| 997 |
template system. |
|---|
| 998 |
|
|---|
| 999 |
For example, if ``model`` has two fields, ``name`` and ``address``:: |
|---|
| 1000 |
|
|---|
| 1001 |
<form action="" method="post"> |
|---|
| 1002 |
<p><label for="id_name">Name:</label> {{ form.name }}</p> |
|---|
| 1003 |
<p><label for="id_address">Address:</label> {{ form.address }}</p> |
|---|
| 1004 |
</form> |
|---|
| 1005 |
|
|---|
| 1006 |
See the `manipulator and formfield documentation`_ for more information |
|---|
| 1007 |
about using ``FormWrapper`` objects in templates. |
|---|
| 1008 |
|
|---|
| 1009 |
* ``object``: The original object being edited. This variable's name |
|---|
| 1010 |
depends on the ``template_object_name`` parameter, which is ``'object'`` |
|---|
| 1011 |
by default. If ``template_object_name`` is ``'foo'``, this variable's |
|---|
| 1012 |
name will be ``foo``. |
|---|
| 1013 |
|
|---|
| 1014 |
``django.views.generic.create_update.delete_object`` |
|---|
| 1015 |
---------------------------------------------------- |
|---|
| 1016 |
|
|---|
| 1017 |
**Description:** |
|---|
| 1018 |
|
|---|
| 1019 |
A view that displays a confirmation page and deletes an existing object. The |
|---|
| 1020 |
given object will only be deleted if the request method is ``POST``. If this |
|---|
| 1021 |
view is fetched via ``GET``, it will display a confirmation page that should |
|---|
| 1022 |
contain a form that POSTs to the same URL. |
|---|
| 1023 |
|
|---|
| 1024 |
**Required arguments:** |
|---|
| 1025 |
|
|---|
| 1026 |
* ``model``: The Django model class of the object that the form will |
|---|
| 1027 |
create. |
|---|
| 1028 |
|
|---|
| 1029 |
* Either ``object_id`` or (``slug`` *and* ``slug_field``) is required. |
|---|
| 1030 |
|
|---|
| 1031 |
If you provide ``object_id``, it should be the value of the primary-key |
|---|
| 1032 |
field for the object being displayed on this page. |
|---|
| 1033 |
|
|---|
| 1034 |
Otherwise, ``slug`` should be the slug of the given object, and |
|---|
| 1035 |
``slug_field`` should be the name of the slug field in the ``QuerySet``'s |
|---|
| 1036 |
model. |
|---|
| 1037 |
|
|---|
| 1038 |
* ``post_delete_redirect``: A URL to which the view will redirect after |
|---|
| 1039 |
deleting the object. |
|---|
| 1040 |
|
|---|
| 1041 |
**Optional arguments:** |
|---|
| 1042 |
|
|---|
| 1043 |
* ``login_required``: A boolean that designates whether a user must be |
|---|
| 1044 |
logged in, in order to see the page and save changes. This hooks into the |
|---|
| 1045 |
Django `authentication system`_. By default, this is ``False``. |
|---|
| 1046 |
|
|---|
| 1047 |
If this is ``True``, and a non-logged-in user attempts to visit this page |
|---|
| 1048 |
or save the form, Django will redirect the request to ``/accounts/login/``. |
|---|
| 1049 |
|
|---|
| 1050 |
* ``template_name``: The full name of a template to use in rendering the |
|---|
| 1051 |
page. This lets you override the default template name (see below). |
|---|
| 1052 |
|
|---|
| 1053 |
* ``template_loader``: The template loader to use when loading the |
|---|
| 1054 |
template. By default, it's ``django.template.loader``. |
|---|
| 1055 |
|
|---|
| 1056 |
* ``extra_context``: A dictionary of values to add to the template |
|---|
| 1057 |
context. By default, this is an empty dictionary. If a value in the |
|---|
| 1058 |
dictionary is callable, the generic view will call it |
|---|
| 1059 |
just before rendering the template. |
|---|
| 1060 |
|
|---|
| 1061 |
* ``context_processors``: A list of template-context processors to apply to |
|---|
| 1062 |
the view's template. See the `RequestContext docs`_. |
|---|
| 1063 |
|
|---|
| 1064 |
* ``template_object_name``: Designates the name of the template variable |
|---|
| 1065 |
to use in the template context. By default, this is ``'object'``. |
|---|
| 1066 |
|
|---|
| 1067 |
**Template name:** |
|---|
| 1068 |
|
|---|
| 1069 |
If ``template_name`` isn't specified, this view will use the template |
|---|
| 1070 |
``<app_label>/<model_name>_confirm_delete.html`` by default. |
|---|
| 1071 |
|
|---|
| 1072 |
**Template context:** |
|---|
| 1073 |
|
|---|
| 1074 |
In addition to ``extra_context``, the template's context will be: |
|---|
| 1075 |
|
|---|
| 1076 |
* ``object``: The original object that's about to be deleted. This |
|---|
| 1077 |
variable's name depends on the ``template_object_name`` parameter, which |
|---|
| 1078 |
is ``'object'`` by default. If ``template_object_name`` is ``'foo'``, |
|---|
| 1079 |
this variable's name will be ``foo``. |
|---|