summaryrefslogtreecommitdiff
path: root/django/forms
diff options
context:
space:
mode:
authorDavid Smith <smithdc@gmail.com>2021-11-06 06:50:29 +0000
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2021-12-09 07:16:33 +0100
commiteba9a9b7f72995206af867600d6685b5405f172a (patch)
treeb073ce1740aa1efd25c61fd2e3d528c590bdb307 /django/forms
parent81739a45b5ae8f534910aaabc7e9b457eaa34163 (diff)
Refs #32338 -- Added Boundfield.legend_tag().
Diffstat (limited to 'django/forms')
-rw-r--r--django/forms/boundfield.py15
-rw-r--r--django/forms/jinja2/django/forms/label.html2
-rw-r--r--django/forms/templates/django/forms/label.html2
3 files changed, 16 insertions, 3 deletions
diff --git a/django/forms/boundfield.py b/django/forms/boundfield.py
index 5bbfcbe41c..62d1823d57 100644
--- a/django/forms/boundfield.py
+++ b/django/forms/boundfield.py
@@ -145,7 +145,7 @@ class BoundField:
initial_value = self.initial
return field.has_changed(initial_value, self.data)
- def label_tag(self, contents=None, attrs=None, label_suffix=None):
+ def label_tag(self, contents=None, attrs=None, label_suffix=None, tag=None):
"""
Wrap the given contents in a <label>, if the field has an ID attribute.
contents should be mark_safe'd to avoid HTML escaping. If contents
@@ -181,9 +181,22 @@ class BoundField:
'label': contents,
'attrs': attrs,
'use_tag': bool(id_),
+ 'tag': tag or 'label',
}
return self.form.render(self.form.template_name_label, context)
+ def legend_tag(self, contents=None, attrs=None, label_suffix=None):
+ """
+ Wrap the given contents in a <legend>, if the field has an ID
+ attribute. Contents should be mark_safe'd to avoid HTML escaping. If
+ contents aren't given, use the field's HTML-escaped label.
+
+ If attrs are given, use them as HTML attributes on the <legend> tag.
+
+ label_suffix overrides the form's label_suffix.
+ """
+ return self.label_tag(contents, attrs, label_suffix, tag='legend')
+
def css_classes(self, extra_classes=None):
"""
Return a string of space-separated CSS classes for this field.
diff --git a/django/forms/jinja2/django/forms/label.html b/django/forms/jinja2/django/forms/label.html
index 7ad5257a71..6ce3885f79 100644
--- a/django/forms/jinja2/django/forms/label.html
+++ b/django/forms/jinja2/django/forms/label.html
@@ -1 +1 @@
-{% if use_tag %}<label{% if attrs %}{% include 'django/forms/attrs.html' %}{% endif %}>{{ label }}</label>{% else %}{{ label }}{% endif %}
+{% if use_tag %}<{{ tag }}{% if attrs %}{% include 'django/forms/attrs.html' %}{% endif %}>{{ label }}</{{ tag }}>{% else %}{{ label }}{% endif %}
diff --git a/django/forms/templates/django/forms/label.html b/django/forms/templates/django/forms/label.html
index eb2a9f7973..0eba630e82 100644
--- a/django/forms/templates/django/forms/label.html
+++ b/django/forms/templates/django/forms/label.html
@@ -1 +1 @@
-{% if use_tag %}<label{% include 'django/forms/attrs.html' %}>{{ label }}</label>{% else %}{{ label }}{% endif %}
+{% if use_tag %}<{{ tag }}{% include 'django/forms/attrs.html' %}>{{ label }}</{{ tag }}>{% else %}{{ label }}{% endif %}