mixins-simple.txt 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. =============
  2. Simple mixins
  3. =============
  4. ContextMixin
  5. ------------
  6. .. class:: django.views.generic.base.ContextMixin
  7. .. versionadded:: 1.5
  8. **Methods**
  9. .. method:: get_context_data(**kwargs)
  10. Returns a dictionary representing the template context. The keyword
  11. arguments provided will make up the returned context. Example usage::
  12. def get_context_data(self, **kwargs):
  13. context = super(RandomNumberView, self).get_context_data(**kwargs)
  14. context['number'] = random.randrange(1, 100)
  15. return context
  16. The template context of all class-based generic views include a
  17. ``view`` variable that points to the ``View`` instance.
  18. .. admonition:: Use ``alters_data`` where appropriate
  19. Note that having the view instance in the template context may
  20. expose potentially hazardous methods to template authors. To
  21. prevent methods like this from being called in the template, set
  22. ``alters_data=True`` on those methods. For more information, read
  23. the documentation on :ref:`rendering a template context
  24. <alters-data-description>`.
  25. TemplateResponseMixin
  26. ---------------------
  27. .. class:: django.views.generic.base.TemplateResponseMixin
  28. Provides a mechanism to construct a
  29. :class:`~django.template.response.TemplateResponse`, given
  30. suitable context. The template to use is configurable and can be
  31. further customized by subclasses.
  32. **Attributes**
  33. .. attribute:: template_name
  34. The full name of a template to use as defined by a string. Not defining
  35. a ``template_name`` will raise a
  36. :class:`django.core.exceptions.ImproperlyConfigured` exception.
  37. .. attribute:: response_class
  38. The response class to be returned by ``render_to_response`` method.
  39. Default is
  40. :class:`TemplateResponse <django.template.response.TemplateResponse>`.
  41. The template and context of ``TemplateResponse`` instances can be
  42. altered later (e.g. in
  43. :ref:`template response middleware <template-response-middleware>`).
  44. .. admonition:: Context processors
  45. ``TemplateResponse`` uses :class:`~django.template.RequestContext`
  46. which means that callables defined in
  47. :setting:`TEMPLATE_CONTEXT_PROCESSORS` may overwrite template
  48. variables defined in your views. For example, if you subclass
  49. :class:`DetailView <django.views.generic.detail.DetailView>` and
  50. set ``context_object_name`` to ``user``, the
  51. ``django.contrib.auth.context_processors.auth`` context processor
  52. will happily overwrite your variable with current user.
  53. If you need custom template loading or custom context object
  54. instantiation, create a ``TemplateResponse`` subclass and assign it to
  55. ``response_class``.
  56. .. attribute:: content_type
  57. .. versionadded:: 1.5
  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 :setting:`DEFAULT_CONTENT_TYPE`.
  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.
  71. If :attr:`template_name` is specified, the default implementation will
  72. return a list containing :attr:`template_name` (if it is specified).