Fixed #34077 -- Added form field rendering.

This commit is contained in:
David Smith 2022-11-02 20:13:16 +00:00 committed by Mariusz Felisiak
parent d33368b4ab
commit cad376f844
16 changed files with 324 additions and 33 deletions

View file

@ -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

View file

@ -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
======================================

View file

@ -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