diff --git a/django/forms/boundfield.py b/django/forms/boundfield.py index 97559fe286..5b8e7e5697 100644 --- a/django/forms/boundfield.py +++ b/django/forms/boundfield.py @@ -192,7 +192,9 @@ class BoundField(RenderableFieldMixin): if id_: id_for_label = widget.id_for_label(id_) if id_for_label: - attrs = {**(attrs or {}), "for": id_for_label} + attrs = attrs or {} + if tag != "legend": + attrs = {**attrs, "for": id_for_label} if self.field.required and hasattr(self.form, "required_css_class"): attrs = attrs or {} if "class" in attrs: diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py index 5b0c4b9a04..8cc1fecf0a 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -3913,7 +3913,7 @@ aria-describedby="id_age_error">""", ) self.assertHTMLEqual( f["field"].legend_tag(), - 'Field:', + 'Field:', ) self.assertHTMLEqual( f["field"].label_tag(attrs={"class": "foo"}), @@ -3921,14 +3921,14 @@ aria-describedby="id_age_error">""", ) self.assertHTMLEqual( f["field"].legend_tag(attrs={"class": "foo"}), - 'Field:', + 'Field:', ) self.assertHTMLEqual( f["field2"].label_tag(), '' ) self.assertHTMLEqual( f["field2"].legend_tag(), - 'Field2:', + "Field2:", ) def test_label_split_datetime_not_displayed(self): @@ -4190,31 +4190,47 @@ aria-describedby="id_age_error">""", boundfield = SomeForm()["field"] - testcases = [ # (args, kwargs, expected) - # without anything: just print the