summaryrefslogtreecommitdiff
path: root/docs/ref/forms
diff options
context:
space:
mode:
authorDavid Smith <smithdc@gmail.com>2022-11-02 20:13:16 +0000
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-03-24 10:16:30 +0100
commitcad376f844c7bdeeee7607a7c0ea8ae52061309b (patch)
tree674bcfb8a4b99c752bff23c029494987ed62f5ef /docs/ref/forms
parentd33368b4ab6ae0c01e83d525f7e1655156a640d1 (diff)
Fixed #34077 -- Added form field rendering.
Diffstat (limited to 'docs/ref/forms')
-rw-r--r--docs/ref/forms/api.txt40
-rw-r--r--docs/ref/forms/fields.txt13
-rw-r--r--docs/ref/forms/renderers.txt31
3 files changed, 84 insertions, 0 deletions
diff --git a/docs/ref/forms/api.txt b/docs/ref/forms/api.txt
index 12754dbae5..4d4f73d0b4 100644
--- a/docs/ref/forms/api.txt
+++ b/docs/ref/forms/api.txt
@@ -1257,6 +1257,16 @@ Attributes of ``BoundField``
>>> print(f["message"].name)
message
+.. attribute:: BoundField.template_name
+
+ .. versionadded:: 5.0
+
+ The name of the template rendered with :meth:`.BoundField.as_field_group`.
+
+ A property returning the value of the
+ :attr:`~django.forms.Field.template_name` if set otherwise
+ :attr:`~django.forms.renderers.BaseRenderer.field_template_name`.
+
.. attribute:: BoundField.use_fieldset
Returns the value of this BoundField widget's ``use_fieldset`` attribute.
@@ -1281,6 +1291,15 @@ Attributes of ``BoundField``
Methods of ``BoundField``
-------------------------
+.. method:: BoundField.as_field_group()
+
+ .. versionadded:: 5.0
+
+ Renders the field using :meth:`.BoundField.render` with default values
+ which renders the ``BoundField``, including its label, help text and errors
+ using the template's :attr:`~django.forms.Field.template_name` if set
+ otherwise :attr:`~django.forms.renderers.BaseRenderer.field_template_name`
+
.. method:: BoundField.as_hidden(attrs=None, **kwargs)
Returns a string of HTML for representing this as an ``<input type="hidden">``.
@@ -1321,6 +1340,13 @@ Methods of ``BoundField``
>>> f["message"].css_classes("foo bar")
'foo bar required'
+.. method:: BoundField.get_context()
+
+ .. versionadded:: 5.0
+
+ Return the template context for rendering the field. The available context
+ is ``field`` being the instance of the bound field.
+
.. method:: BoundField.label_tag(contents=None, attrs=None, label_suffix=None, tag=None)
Renders a label tag for the form field using the template specified by
@@ -1368,6 +1394,20 @@ Methods of ``BoundField``
checkbox widgets where ``<legend>`` may be more appropriate than a
``<label>``.
+.. method:: BoundField.render(template_name=None, context=None, renderer=None)
+
+ .. versionadded:: 5.0
+
+ The render method is called by ``as_field_group``. All arguments are
+ optional and default to:
+
+ * ``template_name``: :attr:`.BoundField.template_name`
+ * ``context``: Value returned by :meth:`.BoundField.get_context`
+ * ``renderer``: Value returned by :attr:`.Form.default_renderer`
+
+ By passing ``template_name`` you can customize the template used for just a
+ single call.
+
.. method:: BoundField.value()
Use this method to render the raw value of this field as it would be rendered
diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt
index 7d975a74d5..476075b936 100644
--- a/docs/ref/forms/fields.txt
+++ b/docs/ref/forms/fields.txt
@@ -337,6 +337,19 @@ using the ``disabled`` HTML attribute so that it won't be editable by users.
Even if a user tampers with the field's value submitted to the server, it will
be ignored in favor of the value from the form's initial data.
+``template_name``
+-----------------
+
+.. attribute:: Field.template_name
+
+.. versionadded:: 5.0
+
+The ``template_name`` argument allows a custom template to be used when the
+field is rendered with :meth:`~django.forms.BoundField.as_field_group`. By
+default this value is set to ``"django/forms/field.html"``. Can be changed per
+field by overriding this attribute or more generally by overriding the default
+template, see also :ref:`overriding-built-in-field-templates`.
+
Checking if the field data has changed
======================================
diff --git a/docs/ref/forms/renderers.txt b/docs/ref/forms/renderers.txt
index 6b4eb95cd7..053b5998c1 100644
--- a/docs/ref/forms/renderers.txt
+++ b/docs/ref/forms/renderers.txt
@@ -59,6 +59,14 @@ should return a rendered templates (as a string) or raise
Defaults to ``"django/forms/formsets/div.html"`` template.
+ .. attribute:: field_template_name
+
+ .. versionadded:: 5.0
+
+ The default name of the template used to render a ``BoundField``.
+
+ Defaults to ``"django/forms/field.html"``
+
.. method:: get_template(template_name)
Subclasses must implement this method with the appropriate template
@@ -162,6 +170,16 @@ forms receive a dictionary with the following values:
* ``hidden_fields``: All hidden bound fields.
* ``errors``: All non field related or hidden field related form errors.
+Context available in field templates
+====================================
+
+.. versionadded:: 5.0
+
+Field templates receive a context from :meth:`.BoundField.get_context`. By
+default, fields receive a dictionary with the following values:
+
+* ``field``: The :class:`~django.forms.BoundField`.
+
Context available in widget templates
=====================================
@@ -201,6 +219,19 @@ To override form templates, you must use the :class:`TemplatesSetting`
renderer. Then overriding widget templates works :doc:`the same as
</howto/overriding-templates>` overriding any other template in your project.
+.. _overriding-built-in-field-templates:
+
+Overriding built-in field templates
+===================================
+
+.. versionadded:: 5.0
+
+:attr:`.Field.template_name`
+
+To override field templates, you must use the :class:`TemplatesSetting`
+renderer. Then overriding field templates works :doc:`the same as
+</howto/overriding-templates>` overriding any other template in your project.
+
.. _overriding-built-in-widget-templates:
Overriding built-in widget templates