renderers.txt 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. ======================
  2. The form rendering API
  3. ======================
  4. .. module:: django.forms.renderers
  5. :synopsis: Built-in form renderers.
  6. Django's form widgets are rendered using Django's :doc:`template engines
  7. system </topics/templates>`.
  8. The form rendering process can be customized at several levels:
  9. * Widgets can specify custom template names.
  10. * Forms and widgets can specify custom renderer classes.
  11. * A widget's template can be overridden by a project. (Reusable applications
  12. typically shouldn't override built-in templates because they might conflict
  13. with a project's custom templates.)
  14. .. _low-level-widget-render-api:
  15. The low-level render API
  16. ========================
  17. The rendering of form templates is controlled by a customizable renderer class.
  18. A custom renderer can be specified by updating the :setting:`FORM_RENDERER`
  19. setting. It defaults to
  20. ``'``:class:`django.forms.renderers.DjangoTemplates`\ ``'``.
  21. You can also provide a custom renderer by setting the
  22. :attr:`.Form.default_renderer` attribute or by using the ``renderer`` argument
  23. of :meth:`.Widget.render`.
  24. Use one of the :ref:`built-in template form renderers
  25. <built-in-template-form-renderers>` or implement your own. Custom renderers
  26. must implement a ``render(template_name, context, request=None)`` method. It
  27. should return a rendered templates (as a string) or raise
  28. :exc:`~django.template.TemplateDoesNotExist`.
  29. .. class:: BaseRenderer
  30. The base class for the built-in form renderers.
  31. .. method:: get_template(template_name)
  32. Subclasses must implement this method with the appropriate template
  33. finding logic.
  34. .. method:: render(template_name, context, request=None)
  35. Renders the given template, or raises
  36. :exc:`~django.template.TemplateDoesNotExist`.
  37. .. _built-in-template-form-renderers:
  38. Built-in-template form renderers
  39. ================================
  40. ``DjangoTemplates``
  41. -------------------
  42. .. class:: DjangoTemplates
  43. This renderer uses a standalone
  44. :class:`~django.template.backends.django.DjangoTemplates`
  45. engine (unconnected to what you might have configured in the
  46. :setting:`TEMPLATES` setting). It loads templates first from the built-in form
  47. templates directory in ``django/forms/templates`` and then from the installed
  48. apps' templates directories using the :class:`app_directories
  49. <django.template.loaders.app_directories.Loader>` loader.
  50. If you want to render templates with customizations from your
  51. :setting:`TEMPLATES` setting, such as context processors for example, use the
  52. :class:`TemplatesSetting` renderer.
  53. ``Jinja2``
  54. ----------
  55. .. class:: Jinja2
  56. This renderer is the same as the :class:`DjangoTemplates` renderer except that
  57. it uses a :class:`~django.template.backends.jinja2.Jinja2` backend. Templates
  58. for the built-in widgets are located in ``django/forms/jinja2`` and installed
  59. apps can provide templates in a ``jinja2`` directory.
  60. To use this backend, all the forms and widgets in your project and its
  61. third-party apps must have Jinja2 templates. Unless you provide your own Jinja2
  62. templates for widgets that don't have any, you can't use this renderer. For
  63. example, :mod:`django.contrib.admin` doesn't include Jinja2 templates for its
  64. widgets due to their usage of Django template tags.
  65. ``TemplatesSetting``
  66. --------------------
  67. .. class:: TemplatesSetting
  68. This renderer gives you complete control of how form and widget templates are
  69. sourced. It uses :func:`~django.template.loader.get_template` to find templates
  70. based on what's configured in the :setting:`TEMPLATES` setting.
  71. Using this renderer along with the built-in templates requires either:
  72. * ``'django.forms'`` in :setting:`INSTALLED_APPS` and at least one engine
  73. with :setting:`APP_DIRS=True <TEMPLATES-APP_DIRS>`.
  74. * Adding the built-in templates directory in :setting:`DIRS <TEMPLATES-DIRS>`
  75. of one of your template engines. To generate that path::
  76. import django
  77. django.__path__[0] + '/forms/templates' # or '/forms/jinja2'
  78. Using this renderer requires you to make sure the form templates your project
  79. needs can be located.
  80. Context available in formset templates
  81. ======================================
  82. .. versionadded:: 4.0
  83. Formset templates receive a context from :meth:`.BaseFormSet.get_context`. By
  84. default, formsets receive a dictionary with the following values:
  85. * ``formset``: The formset instance.
  86. Context available in form templates
  87. ===================================
  88. .. versionadded:: 4.0
  89. Form templates receive a context from :meth:`.Form.get_context`. By default,
  90. forms receive a dictionary with the following values:
  91. * ``form``: The bound form.
  92. * ``fields``: All bound fields, except the hidden fields.
  93. * ``hidden_fields``: All hidden bound fields.
  94. * ``errors``: All non field related or hidden field related form errors.
  95. Context available in widget templates
  96. =====================================
  97. Widget templates receive a context from :meth:`.Widget.get_context`. By
  98. default, widgets receive a single value in the context, ``widget``. This is a
  99. dictionary that contains values like:
  100. * ``name``
  101. * ``value``
  102. * ``attrs``
  103. * ``is_hidden``
  104. * ``template_name``
  105. Some widgets add further information to the context. For instance, all widgets
  106. that subclass ``Input`` defines ``widget['type']`` and :class:`.MultiWidget`
  107. defines ``widget['subwidgets']`` for looping purposes.
  108. .. _overriding-built-in-formset-templates:
  109. Overriding built-in formset templates
  110. =====================================
  111. .. versionadded:: 4.0
  112. :attr:`.BaseFormSet.template_name`
  113. To override formset templates, you must use the :class:`TemplatesSetting`
  114. renderer. Then overriding widget templates works :doc:`the same as
  115. </howto/overriding-templates>` overriding any other template in your project.
  116. .. _overriding-built-in-form-templates:
  117. Overriding built-in form templates
  118. ==================================
  119. .. versionadded:: 4.0
  120. :attr:`.Form.template_name`
  121. To override form templates, you must use the :class:`TemplatesSetting`
  122. renderer. Then overriding widget templates works :doc:`the same as
  123. </howto/overriding-templates>` overriding any other template in your project.
  124. .. _overriding-built-in-widget-templates:
  125. Overriding built-in widget templates
  126. ====================================
  127. Each widget has a ``template_name`` attribute with a value such as
  128. ``input.html``. Built-in widget templates are stored in the
  129. ``django/forms/widgets`` path. You can provide a custom template for
  130. ``input.html`` by defining ``django/forms/widgets/input.html``, for example.
  131. See :ref:`built-in widgets` for the name of each widget's template.
  132. To override widget templates, you must use the :class:`TemplatesSetting`
  133. renderer. Then overriding widget templates works :doc:`the same as
  134. </howto/overriding-templates>` overriding any other template in your project.