| 1 |
============= |
| 2 |
Simple mixins |
| 3 |
============= |
| 4 |
|
| 5 |
``ContextMixin`` |
| 6 |
================ |
| 7 |
|
| 8 |
.. class:: django.views.generic.base.ContextMixin |
| 9 |
|
| 10 |
**Attributes** |
| 11 |
|
| 12 |
.. attribute:: extra_context |
| 13 |
|
| 14 |
A dictionary to include in the context. This is a convenient way of |
| 15 |
specifying some context in |
| 16 |
:meth:`~django.views.generic.base.View.as_view`. Example usage:: |
| 17 |
|
| 18 |
from django.views.generic import TemplateView |
| 19 |
|
| 20 |
TemplateView.as_view(extra_context={"title": "Custom Title"}) |
| 21 |
|
| 22 |
**Methods** |
| 23 |
|
| 24 |
.. method:: get_context_data(**kwargs) |
| 25 |
|
| 26 |
Returns a dictionary representing the template context. The keyword |
| 27 |
arguments provided will make up the returned context. Example usage:: |
| 28 |
|
| 29 |
def get_context_data(self, **kwargs): |
| 30 |
context = super().get_context_data(**kwargs) |
| 31 |
context["number"] = random.randrange(1, 100) |
| 32 |
return context |
| 33 |
|
| 34 |
The template context of all class-based generic views include a |
| 35 |
``view`` variable that points to the ``View`` instance. |
| 36 |
|
| 37 |
.. admonition:: Use ``alters_data`` where appropriate |
| 38 |
|
| 39 |
Note that having the view instance in the template context may |
| 40 |
expose potentially hazardous methods to template authors. To |
| 41 |
prevent methods like this from being called in the template, set |
| 42 |
``alters_data=True`` on those methods. For more information, read |
| 43 |
the documentation on :ref:`rendering a template context |
| 44 |
<alters-data-description>`. |
| 45 |
|
| 46 |
``TemplateResponseMixin`` |
| 47 |
========================= |
| 48 |
|
| 49 |
.. class:: django.views.generic.base.TemplateResponseMixin |
| 50 |
|
| 51 |
Provides a mechanism to construct a |
| 52 |
:class:`~django.template.response.TemplateResponse`, given |
| 53 |
suitable context. The template to use is configurable and can be |
| 54 |
further customized by subclasses. |
| 55 |
|
| 56 |
**Attributes** |
| 57 |
|
| 58 |
.. attribute:: template_name |
| 59 |
|
| 60 |
The full name of a template to use as defined by a string. Not defining |
| 61 |
a ``template_name`` will raise a |
| 62 |
:class:`django.core.exceptions.ImproperlyConfigured` exception. |
| 63 |
|
| 64 |
.. attribute:: template_engine |
| 65 |
|
| 66 |
The :setting:`NAME <TEMPLATES-NAME>` of a template engine to use for |
| 67 |
loading the template. ``template_engine`` is passed as the ``using`` |
| 68 |
keyword argument to ``response_class``. Default is ``None``, which |
| 69 |
tells Django to search for the template in all configured engines. |
| 70 |
|
| 71 |
.. attribute:: response_class |
| 72 |
|
| 73 |
The response class to be returned by ``render_to_response`` method. |
| 74 |
Default is :class:`TemplateResponse |
| 75 |
<django.template.response.TemplateResponse>`. The template and context |
| 76 |
of ``TemplateResponse`` instances can be altered later (e.g. in |
| 77 |
:ref:`template response middleware <template-response-middleware>`). |
| 78 |
|
| 79 |
If you need custom template loading or custom context object |
| 80 |
instantiation, create a ``TemplateResponse`` subclass and assign it to |
| 81 |
``response_class``. |
| 82 |
|
| 83 |
.. attribute:: content_type |
| 84 |
|
| 85 |
The content type to use for the response. ``content_type`` is passed |
| 86 |
as a keyword argument to ``response_class``. Default is ``None`` -- |
| 87 |
meaning that Django uses ``'text/html'``. |
| 88 |
|
| 89 |
**Methods** |
| 90 |
|
| 91 |
.. method:: render_to_response(context, **response_kwargs) |
| 92 |
|
| 93 |
Returns a ``self.response_class`` instance. |
| 94 |
|
| 95 |
If any keyword arguments are provided, they will be passed to the |
| 96 |
constructor of the response class. |
| 97 |
|
| 98 |
Calls :meth:`get_template_names()` to obtain the list of template names |
| 99 |
that will be searched looking for an existent template. |
| 100 |
|
| 101 |
.. method:: get_template_names() |
| 102 |
|
| 103 |
Returns a list of template names to search for when rendering the |
| 104 |
template. The first template that is found will be used. |
| 105 |
|
| 106 |
The default implementation will return a list containing |
| 107 |
:attr:`template_name` (if it is specified). |