mixins-simple.txt 3.9 KB

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