mixins-simple.txt 3.8 KB

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