Loading django/forms/forms.py +2 −3 Original line number Diff line number Diff line Loading @@ -518,8 +518,7 @@ class BoundField(object): """ contents = contents or self.label # Only add the suffix if the label does not end in punctuation. if self.form.label_suffix: if contents[-1] not in ':?.!': if self.form.label_suffix and contents and contents[-1] not in ':?.!': contents = format_html('{0}{1}', contents, self.form.label_suffix) widget = self.field.widget id_ = widget.attrs.get('id') or self.auto_id Loading tests/forms_tests/tests/test_forms.py +7 −0 Original line number Diff line number Diff line Loading @@ -1863,3 +1863,10 @@ class FormsTestCase(TestCase): form = SomeForm() self.assertHTMLEqual(form['custom'].label_tag(), '<label for="custom_id_custom">Custom:</label>') self.assertHTMLEqual(form['empty'].label_tag(), '<label>Empty:</label>') def test_boundfield_empty_label(self): class SomeForm(Form): field = CharField(label='') boundfield = SomeForm()['field'] self.assertHTMLEqual(boundfield.label_tag(), '<label for="id_field"></label>') Loading
django/forms/forms.py +2 −3 Original line number Diff line number Diff line Loading @@ -518,8 +518,7 @@ class BoundField(object): """ contents = contents or self.label # Only add the suffix if the label does not end in punctuation. if self.form.label_suffix: if contents[-1] not in ':?.!': if self.form.label_suffix and contents and contents[-1] not in ':?.!': contents = format_html('{0}{1}', contents, self.form.label_suffix) widget = self.field.widget id_ = widget.attrs.get('id') or self.auto_id Loading
tests/forms_tests/tests/test_forms.py +7 −0 Original line number Diff line number Diff line Loading @@ -1863,3 +1863,10 @@ class FormsTestCase(TestCase): form = SomeForm() self.assertHTMLEqual(form['custom'].label_tag(), '<label for="custom_id_custom">Custom:</label>') self.assertHTMLEqual(form['empty'].label_tag(), '<label>Empty:</label>') def test_boundfield_empty_label(self): class SomeForm(Form): field = CharField(label='') boundfield = SomeForm()['field'] self.assertHTMLEqual(boundfield.label_tag(), '<label for="id_field"></label>')