mixins-simple.txt 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. .. versionchanged:: 1.8
  44. In older versions of Django, ``TemplateResponse`` used
  45. :class:`~django.template.RequestContext` in such a way that values
  46. from template context processors would override template variables
  47. defined in your views. For example, if you subclassed
  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. would overwrite your variable with the current user. Now, for
  52. consistency with the ``render()`` shortcut, values in the context
  53. provided by the class override values from context processors.
  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 :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).