mixins-simple.txt 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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:: template_engine
  37. The :setting:`NAME <TEMPLATES-NAME>` of a template engine to use for
  38. loading the template. ``template_engine`` is passed as the ``using``
  39. keyword argument to ``response_class``. Default is ``None``, which
  40. tells Django to search for the template in all configured engines.
  41. .. attribute:: response_class
  42. The response class to be returned by ``render_to_response`` method.
  43. Default is :class:`TemplateResponse
  44. <django.template.response.TemplateResponse>`. The template and context
  45. of ``TemplateResponse`` instances can be altered later (e.g. in
  46. :ref:`template response middleware <template-response-middleware>`).
  47. If you need custom template loading or custom context object
  48. instantiation, create a ``TemplateResponse`` subclass and assign it to
  49. ``response_class``.
  50. .. attribute:: content_type
  51. The content type to use for the response. ``content_type`` is passed
  52. as a keyword argument to ``response_class``. Default is ``None`` --
  53. meaning that Django uses :setting:`DEFAULT_CONTENT_TYPE`.
  54. **Methods**
  55. .. method:: render_to_response(context, **response_kwargs)
  56. Returns a ``self.response_class`` instance.
  57. If any keyword arguments are provided, they will be passed to the
  58. constructor of the response class.
  59. Calls :meth:`get_template_names()` to obtain the list of template names
  60. that will be searched looking for an existent template.
  61. .. method:: get_template_names()
  62. Returns a list of template names to search for when rendering the
  63. template. The first template that is found will be used.
  64. If :attr:`template_name` is specified, the default implementation will
  65. return a list containing :attr:`template_name` (if it is specified).