From 292f1ea90f90ff140617299a25884c8fda24aa64 Mon Sep 17 00:00:00 2001 From: David Smith Date: Wed, 15 Nov 2023 20:51:00 +0000 Subject: [PATCH] Refs #32819 -- Used auto_id instead of id_for_label as unique identifier for the field. `id_for_label` is blank for widgets with multiple inputs such as radios and multiple checkboxes. Therefore , `help_text` for fields using these widgets cannot currently be associated using `aria-describedby`. `id_for_label` is being used as a guard to avoid incorrectly adding `aria-describedby` to those widgets. This change uses `auto_id` as the unique identified for the fields `help_text`. A guard is added to avoid incorrectly adding `aria-describedby` to inputs by checking the widget's `use_fieldset` attribute. Fields rendered in a `
` should have `aria-describedby` added to the `
` and not every ``. --- django/forms/boundfield.py | 5 +++-- django/forms/jinja2/django/forms/field.html | 2 +- django/forms/jinja2/django/forms/p.html | 2 +- django/forms/jinja2/django/forms/table.html | 2 +- django/forms/jinja2/django/forms/ul.html | 2 +- django/forms/templates/django/forms/field.html | 2 +- django/forms/templates/django/forms/p.html | 2 +- django/forms/templates/django/forms/table.html | 2 +- django/forms/templates/django/forms/ul.html | 2 +- docs/ref/forms/fields.txt | 6 +++--- docs/releases/5.0.txt | 6 +++--- docs/topics/forms/index.txt | 2 +- 12 files changed, 18 insertions(+), 17 deletions(-) diff --git a/django/forms/boundfield.py b/django/forms/boundfield.py index 98ac5b8208c..d56fa0ea3e1 100644 --- a/django/forms/boundfield.py +++ b/django/forms/boundfield.py @@ -296,9 +296,10 @@ class BoundField(RenderableFieldMixin): not attrs.get("aria-describedby") and not widget.attrs.get("aria-describedby") and self.field.help_text - and self.id_for_label + and not self.use_fieldset + and self.auto_id ): - attrs["aria-describedby"] = f"{self.id_for_label}_helptext" + attrs["aria-describedby"] = f"{self.auto_id}_helptext" return attrs @property diff --git a/django/forms/jinja2/django/forms/field.html b/django/forms/jinja2/django/forms/field.html index c8943d9481e..21035b72e46 100644 --- a/django/forms/jinja2/django/forms/field.html +++ b/django/forms/jinja2/django/forms/field.html @@ -4,7 +4,7 @@ {% else %} {% if field.label %}{{ field.label_tag() }}{% endif %} {% endif %} -{% if field.help_text %}
{{ field.help_text|safe }}
{% endif %} +{% if field.help_text %}
{{ field.help_text|safe }}
{% endif %} {{ field.errors }} {{ field }} {% if field.use_fieldset %}
{% endif %} diff --git a/django/forms/jinja2/django/forms/p.html b/django/forms/jinja2/django/forms/p.html index 4db46058b52..a7d0098b447 100644 --- a/django/forms/jinja2/django/forms/p.html +++ b/django/forms/jinja2/django/forms/p.html @@ -8,7 +8,7 @@ {% if field.label %}{{ field.label_tag() }}{% endif %} {{ field }} {% if field.help_text %} - {{ field.help_text|safe }} + {{ field.help_text|safe }} {% endif %} {% if loop.last %} {% for field in hidden_fields %}{{ field }}{% endfor %} diff --git a/django/forms/jinja2/django/forms/table.html b/django/forms/jinja2/django/forms/table.html index 18fa63f8a7c..a817d555224 100644 --- a/django/forms/jinja2/django/forms/table.html +++ b/django/forms/jinja2/django/forms/table.html @@ -16,7 +16,7 @@ {{ field }} {% if field.help_text %}
- {{ field.help_text|safe }} + {{ field.help_text|safe }} {% endif %} {% if loop.last %} {% for field in hidden_fields %}{{ field }}{% endfor %} diff --git a/django/forms/jinja2/django/forms/ul.html b/django/forms/jinja2/django/forms/ul.html index 42c46847fdb..9514e09729d 100644 --- a/django/forms/jinja2/django/forms/ul.html +++ b/django/forms/jinja2/django/forms/ul.html @@ -12,7 +12,7 @@ {% if field.label %}{{ field.label_tag() }}{% endif %} {{ field }} {% if field.help_text %} - {{ field.help_text|safe }} + {{ field.help_text|safe }} {% endif %} {% if loop.last %} {% for field in hidden_fields %}{{ field }}{% endfor %} diff --git a/django/forms/templates/django/forms/field.html b/django/forms/templates/django/forms/field.html index 72fb357b71c..f8b0cdedd56 100644 --- a/django/forms/templates/django/forms/field.html +++ b/django/forms/templates/django/forms/field.html @@ -4,7 +4,7 @@ {% else %} {% if field.label %}{{ field.label_tag }}{% endif %} {% endif %} -{% if field.help_text %}
{{ field.help_text|safe }}
{% endif %} +{% if field.help_text %}
{{ field.help_text|safe }}
{% endif %} {{ field.errors }} {{ field }} {% if field.use_fieldset %}
{% endif %} diff --git a/django/forms/templates/django/forms/p.html b/django/forms/templates/django/forms/p.html index 829c42eca6a..89f0a2ba03f 100644 --- a/django/forms/templates/django/forms/p.html +++ b/django/forms/templates/django/forms/p.html @@ -8,7 +8,7 @@ {% if field.label %}{{ field.label_tag }}{% endif %} {{ field }} {% if field.help_text %} - {{ field.help_text|safe }} + {{ field.help_text|safe }} {% endif %} {% if forloop.last %} {% for field in hidden_fields %}{{ field }}{% endfor %} diff --git a/django/forms/templates/django/forms/table.html b/django/forms/templates/django/forms/table.html index 5d41bc44026..4fbd3b9899e 100644 --- a/django/forms/templates/django/forms/table.html +++ b/django/forms/templates/django/forms/table.html @@ -16,7 +16,7 @@ {{ field }} {% if field.help_text %}
- {{ field.help_text|safe }} + {{ field.help_text|safe }} {% endif %} {% if forloop.last %} {% for field in hidden_fields %}{{ field }}{% endfor %} diff --git a/django/forms/templates/django/forms/ul.html b/django/forms/templates/django/forms/ul.html index 7383b962357..d78a79aeca9 100644 --- a/django/forms/templates/django/forms/ul.html +++ b/django/forms/templates/django/forms/ul.html @@ -12,7 +12,7 @@ {% if field.label %}{{ field.label_tag }}{% endif %} {{ field }} {% if field.help_text %} - {{ field.help_text|safe }} + {{ field.help_text|safe }} {% endif %} {% if forloop.last %} {% for field in hidden_fields %}{{ field }}{% endfor %} diff --git a/docs/ref/forms/fields.txt b/docs/ref/forms/fields.txt index df9732f1af0..9dff2d4007f 100644 --- a/docs/ref/forms/fields.txt +++ b/docs/ref/forms/fields.txt @@ -283,9 +283,9 @@ fields. We've specified ``auto_id=False`` to simplify the output:
Sender:
A valid email address, please.
Cc myself:
-When a field has help text and :attr:`~django.forms.BoundField.id_for_label` -returns a value, we associate ``help_text`` with the input using the -``aria-describedby`` HTML attribute: +When a field has help text and the widget is not rendered in a ``
``, +``aria-describedby`` is added to the ```` to associate it to the +help text: .. code-block:: pycon diff --git a/docs/releases/5.0.txt b/docs/releases/5.0.txt index db75a6b0a39..269e3b382e7 100644 --- a/docs/releases/5.0.txt +++ b/docs/releases/5.0.txt @@ -61,7 +61,7 @@ For example, the template below:
{{ form.name.label_tag }} {% if form.name.help_text %} -
+
{{ form.name.help_text|safe }}
{% endif %} @@ -71,7 +71,7 @@ For example, the template below:
{{ form.email.label_tag }} {% if form.email.help_text %} -
+
{{ form.email.help_text|safe }}
{% endif %} @@ -81,7 +81,7 @@ For example, the template below:
{{ form.password.label_tag }} {% if form.password.help_text %} -
+
{{ form.password.help_text|safe }}
{% endif %} diff --git a/docs/topics/forms/index.txt b/docs/topics/forms/index.txt index 7b27e98996c..55d032c9bb4 100644 --- a/docs/topics/forms/index.txt +++ b/docs/topics/forms/index.txt @@ -723,7 +723,7 @@ loop: {{ field.errors }} {{ field.label_tag }} {{ field }} {% if field.help_text %} -

+

{{ field.help_text|safe }}

{% endif %}