2012-06-11 10:34:00 +02:00
|
|
|
=============
|
|
|
|
Simple mixins
|
|
|
|
=============
|
|
|
|
|
2016-01-24 22:26:11 +01:00
|
|
|
``ContextMixin``
|
|
|
|
================
|
2012-08-11 08:07:15 +02:00
|
|
|
|
2012-06-11 10:34:00 +02:00
|
|
|
.. class:: django.views.generic.base.ContextMixin
|
|
|
|
|
2017-07-06 16:34:54 +02:00
|
|
|
**Attributes**
|
|
|
|
|
|
|
|
.. attribute:: extra_context
|
|
|
|
|
|
|
|
A dictionary to include in the context. This is a convenient way of
|
2019-06-17 16:54:55 +02:00
|
|
|
specifying some context in
|
2017-07-06 16:34:54 +02:00
|
|
|
:meth:`~django.views.generic.base.View.as_view`. Example usage::
|
|
|
|
|
|
|
|
from django.views.generic import TemplateView
|
2023-02-28 20:53:28 +01:00
|
|
|
|
2017-07-06 16:34:54 +02:00
|
|
|
TemplateView.as_view(extra_context={"title": "Custom Title"})
|
|
|
|
|
2012-06-11 10:34:00 +02:00
|
|
|
**Methods**
|
|
|
|
|
|
|
|
.. method:: get_context_data(**kwargs)
|
|
|
|
|
2012-08-18 15:46:17 +02:00
|
|
|
Returns a dictionary representing the template context. The keyword
|
2012-09-08 19:46:08 +02:00
|
|
|
arguments provided will make up the returned context. Example usage::
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
2017-01-22 07:57:14 +01:00
|
|
|
context = super().get_context_data(**kwargs)
|
2012-09-08 19:46:08 +02:00
|
|
|
context["number"] = random.randrange(1, 100)
|
|
|
|
return context
|
2012-08-18 15:46:17 +02: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 10:34:00 +02:00
|
|
|
|
2016-01-24 22:26:11 +01:00
|
|
|
``TemplateResponseMixin``
|
|
|
|
=========================
|
2012-08-11 08:07:15 +02:00
|
|
|
|
2012-06-11 10:34:00 +02: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-08 19:46:08 +02:00
|
|
|
**Attributes**
|
|
|
|
|
|
|
|
.. attribute:: template_name
|
|
|
|
|
|
|
|
The full name of a template to use as defined by a string. Not defining
|
2013-01-01 14:12:42 +01:00
|
|
|
a ``template_name`` will raise a
|
2012-09-08 19:46:08 +02:00
|
|
|
:class:`django.core.exceptions.ImproperlyConfigured` exception.
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2015-01-26 21:57:10 +01:00
|
|
|
.. attribute:: template_engine
|
|
|
|
|
|
|
|
The :setting:`NAME <TEMPLATES-NAME>` of a template engine to use for
|
|
|
|
loading the template. ``template_engine`` is passed as the ``using``
|
|
|
|
keyword argument to ``response_class``. Default is ``None``, which
|
|
|
|
tells Django to search for the template in all configured engines.
|
|
|
|
|
2012-06-11 10:34:00 +02:00
|
|
|
.. attribute:: response_class
|
|
|
|
|
|
|
|
The response class to be returned by ``render_to_response`` method.
|
2015-09-12 22:27:30 +02:00
|
|
|
Default is :class:`TemplateResponse
|
|
|
|
<django.template.response.TemplateResponse>`. The template and context
|
|
|
|
of ``TemplateResponse`` instances can be altered later (e.g. in
|
2012-06-11 10:34:00 +02:00
|
|
|
:ref:`template response middleware <template-response-middleware>`).
|
|
|
|
|
|
|
|
If you need custom template loading or custom context object
|
|
|
|
instantiation, create a ``TemplateResponse`` subclass and assign it to
|
|
|
|
``response_class``.
|
|
|
|
|
2013-01-30 21:26:17 +01: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`` --
|
2018-12-28 01:49:03 +01:00
|
|
|
meaning that Django uses ``'text/html'``.
|
2013-01-30 21:26:17 +01:00
|
|
|
|
2012-09-08 19:46:08 +02:00
|
|
|
**Methods**
|
|
|
|
|
2012-06-11 10:34:00 +02:00
|
|
|
.. method:: render_to_response(context, **response_kwargs)
|
|
|
|
|
|
|
|
Returns a ``self.response_class`` instance.
|
|
|
|
|
2012-09-08 19:46:08 +02:00
|
|
|
If any keyword arguments are provided, they will be passed to the
|
|
|
|
constructor of the response class.
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2013-01-01 14:12:42 +01: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 10:34:00 +02:00
|
|
|
|
|
|
|
.. method:: get_template_names()
|
|
|
|
|
|
|
|
Returns a list of template names to search for when rendering the
|
2016-03-21 13:09:13 +01:00
|
|
|
template. The first template that is found will be used.
|
2012-06-11 10:34:00 +02:00
|
|
|
|
2020-06-01 10:16:45 +02:00
|
|
|
The default implementation will return a list containing
|
|
|
|
:attr:`template_name` (if it is specified).
|