Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Fixed #18134 -- Inconsistent label_tag generation on form fields #141

Closed
wants to merge 2 commits into from

2 participants

Gabe Jackson Tim Graham
Gabe Jackson

There was an inconsistency between how the label_tag for forms were
generated depending on which method was used: as_p, as_ul and as_table
contained code to append the label_suffix where as label_tag called on a
form field directly did NOT append the label_suffix. The code for
appending the label_suffix has been moved in to the label_tag code of
the field and the HTML generation code for as_p, as_ul and as_table now
calls this code as well. CAUTION: This may be be "backwards incompatible
change" because users who have added the label_suffix manually in their
templates may now get double label_suffix characters in their forms.

Also some test cases regarding the label_tag output were inconsistent.
Some expected Label: and some expected the label_suffix
outside of the tag: Label:
The format has now been unified to keep the label_suffix inside the
tag: Label:. If the label_suffix is not needed,
the form can still be constructed with label_suffix=''.

added some commits June 08, 2012
Gabe Jackson Fixed #18134 -- Inconsistent label_tag generation on form fields
There was an inconsistency between how the label_tag for forms were
generated depending on which method was used: as_p, as_ul and as_table
contained code to append the label_suffix where as label_tag called on a
form field directly did NOT append the label_suffix. The code for
appending the label_suffix has been moved in to the label_tag code of
the field and the HTML generation code for as_p, as_ul and as_table now
calls this code as well. CAUTION: This may be be "backwards incompatible
change" because users who have added the label_suffix manually in their
templates may now get double label_suffix characters in their forms.

Also some test cases regarding the label_tag output were inconsistent.
Some expected <label>Label:</label> and some expected the label_suffix
outside of the <label> tag: <label>Label</label>:
The format has now been unified to keep the label_suffix inside the
<label> tag: <label>Label:</label>. If the label_suffix is not needed,
the form can still be constructed with label_suffix=''.
8fff02e
Gabe Jackson Documentation for ticket #18134
Removed the extra ':' from the template examples in the forms
documentation. Noted that the label_tag template tag also outputs the
label_suffix.
8c5ef94
Tim Graham
Owner

I've added additional docs for this change and updated it to merge cleanly: #1252

Tim Graham timgraham closed this June 07, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 1 author.

Jun 08, 2012
Gabe Jackson Fixed #18134 -- Inconsistent label_tag generation on form fields
There was an inconsistency between how the label_tag for forms were
generated depending on which method was used: as_p, as_ul and as_table
contained code to append the label_suffix where as label_tag called on a
form field directly did NOT append the label_suffix. The code for
appending the label_suffix has been moved in to the label_tag code of
the field and the HTML generation code for as_p, as_ul and as_table now
calls this code as well. CAUTION: This may be be "backwards incompatible
change" because users who have added the label_suffix manually in their
templates may now get double label_suffix characters in their forms.

Also some test cases regarding the label_tag output were inconsistent.
Some expected <label>Label:</label> and some expected the label_suffix
outside of the <label> tag: <label>Label</label>:
The format has now been unified to keep the label_suffix inside the
<label> tag: <label>Label:</label>. If the label_suffix is not needed,
the form can still be constructed with label_suffix=''.
8fff02e
Gabe Jackson Documentation for ticket #18134
Removed the extra ':' from the template examples in the forms
documentation. Noted that the label_tag template tag also outputs the
label_suffix.
8c5ef94
This page is out of date. Refresh to see the latest.
9  django/forms/forms.py
@@ -163,11 +163,6 @@ def _html_output(self, normal_row, error_row, row_ender, help_text_html, errors_
163 163
 
164 164
                 if bf.label:
165 165
                     label = conditional_escape(force_unicode(bf.label))
166  
-                    # Only add the suffix if the label does not end in
167  
-                    # punctuation.
168  
-                    if self.label_suffix:
169  
-                        if label[-1] not in ':?.!':
170  
-                            label += self.label_suffix
171 166
                     label = bf.label_tag(label) or ''
172 167
                 else:
173 168
                     label = ''
@@ -504,6 +499,10 @@ def label_tag(self, contents=None, attrs=None):
504 499
         If attrs are given, they're used as HTML attributes on the <label> tag.
505 500
         """
506 501
         contents = contents or conditional_escape(self.label)
  502
+        # Only add the suffix if the label does not end in punctuation.
  503
+        if self.form.label_suffix:
  504
+            if contents[-1] not in ':?.!':
  505
+                contents += self.form.label_suffix
507 506
         widget = self.field.widget
508 507
         id_ = widget.attrs.get('id') or self.auto_id
509 508
         if id_:
11  docs/topics/forms/index.txt
@@ -289,7 +289,7 @@ loop::
289 289
         {% for field in form %}
290 290
             <div class="fieldWrapper">
291 291
                 {{ field.errors }}
292  
-                {{ field.label_tag }}: {{ field }}
  292
+                {{ field.label_tag }} {{ field }}
293 293
             </div>
294 294
         {% endfor %}
295 295
         <p><input type="submit" value="Send message" /></p>
@@ -303,8 +303,9 @@ templates:
303 303
     The label of the field, e.g. ``Email address``.
304 304
 
305 305
 ``{{ field.label_tag }}``
306  
-    The field's label wrapped in the appropriate HTML ``<label>`` tag,
307  
-    e.g. ``<label for="id_email">Email address</label>``
  306
+    The field's label wrapped in the appropriate HTML ``<label>`` tag. This
  307
+    includes the form's label_suffix, e.g. 
  308
+    ``<label for="id_email">Email address:</label>``
308 309
 
309 310
 ``{{ field.value }}``
310 311
     The value of the field. e.g ``someone@example.com``
@@ -356,7 +357,7 @@ these two methods::
356 357
         {% for field in form.visible_fields %}
357 358
             <div class="fieldWrapper">
358 359
                 {{ field.errors }}
359  
-                {{ field.label_tag }}: {{ field }}
  360
+                {{ field.label_tag }} {{ field }}
360 361
             </div>
361 362
         {% endfor %}
362 363
         <p><input type="submit" value="Send message" /></p>
@@ -384,7 +385,7 @@ using the :ttag:`include` tag to reuse it in other templates::
384 385
     {% for field in form %}
385 386
         <div class="fieldWrapper">
386 387
             {{ field.errors }}
387  
-            {{ field.label_tag }}: {{ field }}
  388
+            {{ field.label_tag }} {{ field }}
388 389
         </div>
389 390
     {% endfor %}
390 391
 
2  docs/topics/forms/modelforms.txt
@@ -791,7 +791,7 @@ Third, you can manually render each field::
791 791
         {{ formset.management_form }}
792 792
         {% for form in formset %}
793 793
             {% for field in form %}
794  
-                {{ field.label_tag }}: {{ field }}
  794
+                {{ field.label_tag }} {{ field }}
795 795
             {% endfor %}
796 796
         {% endfor %}
797 797
     </form>
4  tests/regressiontests/admin_util/tests.py
@@ -272,7 +272,7 @@ class MyForm(forms.Form):
272 272
         self.assertEqual(helpers.AdminField(form, 'text', is_first=False).label_tag(),
273 273
                          '<label for="id_text" class="required inline"><i>text</i>:</label>')
274 274
         self.assertEqual(helpers.AdminField(form, 'cb', is_first=False).label_tag(),
275  
-                         '<label for="id_cb" class="vCheckboxLabel required inline"><i>cb</i></label>')
  275
+                         '<label for="id_cb" class="vCheckboxLabel required inline"><i>cb</i>:</label>')
276 276
 
277 277
         # normal strings needs to be escaped
278 278
         class MyForm(forms.Form):
@@ -283,4 +283,4 @@ class MyForm(forms.Form):
283 283
         self.assertEqual(helpers.AdminField(form, 'text', is_first=False).label_tag(),
284 284
                          '<label for="id_text" class="required inline">&amp;text:</label>')
285 285
         self.assertEqual(helpers.AdminField(form, 'cb', is_first=False).label_tag(),
286  
-                         '<label for="id_cb" class="vCheckboxLabel required inline">&amp;cb</label>')
  286
+                         '<label for="id_cb" class="vCheckboxLabel required inline">&amp;cb:</label>')
24  tests/regressiontests/forms/tests/forms.py
@@ -1583,9 +1583,9 @@ def clean(self):
1583 1583
         # Recall from above that passing the "auto_id" argument to a Form gives each
1584 1584
         # field an "id" attribute.
1585 1585
         t = Template('''<form action="">
1586  
-<p>{{ form.username.label_tag }}: {{ form.username }}</p>
1587  
-<p>{{ form.password1.label_tag }}: {{ form.password1 }}</p>
1588  
-<p>{{ form.password2.label_tag }}: {{ form.password2 }}</p>
  1586
+<p>{{ form.username.label_tag }} {{ form.username }}</p>
  1587
+<p>{{ form.password1.label_tag }} {{ form.password1 }}</p>
  1588
+<p>{{ form.password2.label_tag }} {{ form.password2 }}</p>
1589 1589
 <input type="submit" />
1590 1590
 </form>''')
1591 1591
         self.assertHTMLEqual(t.render(Context({'form': UserRegistration(auto_id=False)})), """<form action="">
@@ -1595,18 +1595,18 @@ def clean(self):
1595 1595
 <input type="submit" />
1596 1596
 </form>""")
1597 1597
         self.assertHTMLEqual(t.render(Context({'form': UserRegistration(auto_id='id_%s')})), """<form action="">
1598  
-<p><label for="id_username">Username</label>: <input id="id_username" type="text" name="username" maxlength="10" /></p>
1599  
-<p><label for="id_password1">Password1</label>: <input type="password" name="password1" id="id_password1" /></p>
1600  
-<p><label for="id_password2">Password2</label>: <input type="password" name="password2" id="id_password2" /></p>
  1598
+<p><label for="id_username">Username:</label> <input id="id_username" type="text" name="username" maxlength="10" /></p>
  1599
+<p><label for="id_password1">Password1:</label> <input type="password" name="password1" id="id_password1" /></p>
  1600
+<p><label for="id_password2">Password2:</label> <input type="password" name="password2" id="id_password2" /></p>
1601 1601
 <input type="submit" />
1602 1602
 </form>""")
1603 1603
 
1604 1604
         # User form.[field].help_text to output a field's help text. If the given field
1605 1605
         # does not have help text, nothing will be output.
1606 1606
         t = Template('''<form action="">
1607  
-<p>{{ form.username.label_tag }}: {{ form.username }}<br />{{ form.username.help_text }}</p>
1608  
-<p>{{ form.password1.label_tag }}: {{ form.password1 }}</p>
1609  
-<p>{{ form.password2.label_tag }}: {{ form.password2 }}</p>
  1607
+<p>{{ form.username.label_tag }} {{ form.username }}<br />{{ form.username.help_text }}</p>
  1608
+<p>{{ form.password1.label_tag }} {{ form.password1 }}</p>
  1609
+<p>{{ form.password2.label_tag }} {{ form.password2 }}</p>
1610 1610
 <input type="submit" />
1611 1611
 </form>''')
1612 1612
         self.assertHTMLEqual(t.render(Context({'form': UserRegistration(auto_id=False)})), """<form action="">
@@ -1626,9 +1626,9 @@ def clean(self):
1626 1626
             form_output.append(bf.label_tag(attrs={'class': 'pretty'}))
1627 1627
 
1628 1628
         expected_form_output = [
1629  
-            '<label for="id_username" class="pretty">Username</label>',
1630  
-            '<label for="id_password1" class="pretty">Password1</label>',
1631  
-            '<label for="id_password2" class="pretty">Password2</label>',
  1629
+            '<label for="id_username" class="pretty">Username:</label>',
  1630
+            '<label for="id_password1" class="pretty">Password1:</label>',
  1631
+            '<label for="id_password2" class="pretty">Password2:</label>',
1632 1632
         ]
1633 1633
         self.assertEqual(len(form_output), len(expected_form_output))
1634 1634
         for i in range(len(form_output)):
4  tests/regressiontests/forms/tests/regressions.py
@@ -44,8 +44,8 @@ class SomeForm(Form):
44 44
             field_2 = CharField(max_length=10, label=ugettext_lazy('field_2'), widget=TextInput(attrs={'id': 'field_2_id'}))
45 45
 
46 46
         f = SomeForm()
47  
-        self.assertHTMLEqual(f['field_1'].label_tag(), '<label for="id_field_1">field_1</label>')
48  
-        self.assertHTMLEqual(f['field_2'].label_tag(), '<label for="field_2_id">field_2</label>')
  47
+        self.assertHTMLEqual(f['field_1'].label_tag(), '<label for="id_field_1">field_1:</label>')
  48
+        self.assertHTMLEqual(f['field_2'].label_tag(), '<label for="field_2_id">field_2:</label>')
49 49
 
50 50
         # Unicode decoding problems...
51 51
         GENDERS = (('\xc5', 'En tied\xe4'), ('\xf8', 'Mies'), ('\xdf', 'Nainen'))
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.