2
0

mixins-simple.txt 3.8 KB

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