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