2
0

mixins-simple.txt 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. =============
  2. Simple mixins
  3. =============
  4. ContextMixin
  5. ------------
  6. .. class:: django.views.generic.base.ContextMixin
  7. .. versionadded:: 1.5
  8. **classpath**
  9. ``django.views.generic.base.ContextMixin``
  10. **Methods**
  11. .. method:: get_context_data(**kwargs)
  12. Returns a dictionary representing the template context. The keyword
  13. arguments provided will make up the returned context.
  14. The template context of all class-based generic views include a
  15. ``view`` variable that points to the ``View`` instance.
  16. .. admonition:: Use ``alters_data`` where appropriate
  17. Note that having the view instance in the template context may
  18. expose potentially hazardous methods to template authors. To
  19. prevent methods like this from being called in the template, set
  20. ``alters_data=True`` on those methods. For more information, read
  21. the documentation on :ref:`rendering a template context
  22. <alters-data-description>`.
  23. TemplateResponseMixin
  24. ---------------------
  25. .. class:: django.views.generic.base.TemplateResponseMixin
  26. Provides a mechanism to construct a
  27. :class:`~django.template.response.TemplateResponse`, given
  28. suitable context. The template to use is configurable and can be
  29. further customized by subclasses.
  30. **Methods and Attributes**
  31. .. attribute:: response_class
  32. The response class to be returned by ``render_to_response`` method.
  33. Default is
  34. :class:`TemplateResponse <django.template.response.TemplateResponse>`.
  35. The template and context of ``TemplateResponse`` instances can be
  36. altered later (e.g. in
  37. :ref:`template response middleware <template-response-middleware>`).
  38. If you need custom template loading or custom context object
  39. instantiation, create a ``TemplateResponse`` subclass and assign it to
  40. ``response_class``.
  41. .. method:: render_to_response(context, **response_kwargs)
  42. Returns a ``self.response_class`` instance.
  43. If any keyword arguments are provided, they will be
  44. passed to the constructor of the response class.
  45. Calls :meth:`~TemplateResponseMixin.get_template_names()` to obtain the
  46. list of template names that will be searched looking for an existent
  47. template.
  48. .. method:: get_template_names()
  49. Returns a list of template names to search for when rendering the
  50. template.
  51. If :attr:`TemplateResponseMixin.template_name` is specified, the
  52. default implementation will return a list containing
  53. :attr:`TemplateResponseMixin.template_name` (if it is specified).