renderers.txt 4.9 KB

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