2012-06-11 16:34:00 +08:00
|
|
|
=============
|
|
|
|
Simple mixins
|
|
|
|
=============
|
|
|
|
|
2012-08-11 14:07:15 +08:00
|
|
|
ContextMixin
|
|
|
|
------------
|
|
|
|
|
2012-06-11 16:34:00 +08:00
|
|
|
.. class:: django.views.generic.base.ContextMixin
|
|
|
|
|
|
|
|
**Methods**
|
|
|
|
|
|
|
|
.. method:: get_context_data(**kwargs)
|
|
|
|
|
2012-08-18 21:46:17 +08:00
|
|
|
Returns a dictionary representing the template context. The keyword
|
2012-09-09 01:46:08 +08:00
|
|
|
arguments provided will make up the returned context. Example usage::
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(RandomNumberView, self).get_context_data(**kwargs)
|
|
|
|
context['number'] = random.randrange(1, 100)
|
|
|
|
return context
|
2012-08-18 21:46:17 +08:00
|
|
|
|
|
|
|
The template context of all class-based generic views include a
|
|
|
|
``view`` variable that points to the ``View`` instance.
|
|
|
|
|
|
|
|
.. admonition:: Use ``alters_data`` where appropriate
|
|
|
|
|
|
|
|
Note that having the view instance in the template context may
|
|
|
|
expose potentially hazardous methods to template authors. To
|
|
|
|
prevent methods like this from being called in the template, set
|
|
|
|
``alters_data=True`` on those methods. For more information, read
|
|
|
|
the documentation on :ref:`rendering a template context
|
|
|
|
<alters-data-description>`.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
2012-08-11 14:07:15 +08:00
|
|
|
TemplateResponseMixin
|
|
|
|
---------------------
|
|
|
|
|
2012-06-11 16:34:00 +08:00
|
|
|
.. class:: django.views.generic.base.TemplateResponseMixin
|
|
|
|
|
|
|
|
Provides a mechanism to construct a
|
|
|
|
:class:`~django.template.response.TemplateResponse`, given
|
|
|
|
suitable context. The template to use is configurable and can be
|
|
|
|
further customized by subclasses.
|
|
|
|
|
2012-09-09 01:46:08 +08:00
|
|
|
**Attributes**
|
|
|
|
|
|
|
|
.. attribute:: template_name
|
|
|
|
|
|
|
|
The full name of a template to use as defined by a string. Not defining
|
2013-01-01 21:12:42 +08:00
|
|
|
a ``template_name`` will raise a
|
2012-09-09 01:46:08 +08:00
|
|
|
:class:`django.core.exceptions.ImproperlyConfigured` exception.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
.. attribute:: response_class
|
|
|
|
|
|
|
|
The response class to be returned by ``render_to_response`` method.
|
|
|
|
Default is
|
|
|
|
:class:`TemplateResponse <django.template.response.TemplateResponse>`.
|
|
|
|
The template and context of ``TemplateResponse`` instances can be
|
|
|
|
altered later (e.g. in
|
|
|
|
:ref:`template response middleware <template-response-middleware>`).
|
|
|
|
|
2014-12-18 09:02:15 +08:00
|
|
|
.. versionchanged:: 1.8
|
|
|
|
|
|
|
|
In older versions of Django, ``TemplateResponse`` used
|
2014-12-18 06:36:32 +08:00
|
|
|
:class:`~django.template.RequestContext` in such a way that values
|
|
|
|
from template context processors would override template variables
|
|
|
|
defined in your views. For example, if you subclassed
|
|
|
|
:class:`DetailView <django.views.generic.detail.DetailView>` and
|
2013-05-18 18:32:48 +08:00
|
|
|
set ``context_object_name`` to ``user``, the
|
|
|
|
``django.contrib.auth.context_processors.auth`` context processor
|
2014-12-18 09:02:15 +08:00
|
|
|
would overwrite your variable with the current user. Now, for
|
|
|
|
consistency with the ``render()`` shortcut, values in the context
|
|
|
|
provided by the class override values from context processors.
|
2013-05-18 18:32:48 +08:00
|
|
|
|
2012-06-11 16:34:00 +08:00
|
|
|
If you need custom template loading or custom context object
|
|
|
|
instantiation, create a ``TemplateResponse`` subclass and assign it to
|
|
|
|
``response_class``.
|
|
|
|
|
2013-01-31 04:26:17 +08:00
|
|
|
.. attribute:: content_type
|
|
|
|
|
|
|
|
The content type to use for the response. ``content_type`` is passed
|
|
|
|
as a keyword argument to ``response_class``. Default is ``None`` --
|
|
|
|
meaning that Django uses :setting:`DEFAULT_CONTENT_TYPE`.
|
|
|
|
|
2012-09-09 01:46:08 +08:00
|
|
|
**Methods**
|
|
|
|
|
2012-06-11 16:34:00 +08:00
|
|
|
.. method:: render_to_response(context, **response_kwargs)
|
|
|
|
|
|
|
|
Returns a ``self.response_class`` instance.
|
|
|
|
|
2012-09-09 01:46:08 +08:00
|
|
|
If any keyword arguments are provided, they will be passed to the
|
|
|
|
constructor of the response class.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
Calls :meth:`get_template_names()` to obtain the list of template names
|
|
|
|
that will be searched looking for an existent template.
|
2012-06-11 16:34:00 +08:00
|
|
|
|
|
|
|
.. method:: get_template_names()
|
|
|
|
|
|
|
|
Returns a list of template names to search for when rendering the
|
|
|
|
template.
|
|
|
|
|
2013-01-01 21:12:42 +08:00
|
|
|
If :attr:`template_name` is specified, the default implementation will
|
|
|
|
return a list containing :attr:`template_name` (if it is specified).
|