Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.6.x] Fixed #20805 -- Removed an extra colon beside checkboxes in t…

…he admin.

Thanks CollinAnderson for the report.

Backport of 8676318 from master
  • Loading branch information...
commit dbbd2b1272e5a3ce7ab4b39b9a74f4ea725b104f 1 parent 5cc1ea4
Tim Graham authored July 26, 2013
8  django/contrib/admin/helpers.py
@@ -125,14 +125,16 @@ def label_tag(self):
125 125
         contents = conditional_escape(force_text(self.field.label))
126 126
         if self.is_checkbox:
127 127
             classes.append('vCheckboxLabel')
128  
-        else:
129  
-            contents += ':'
  128
+
130 129
         if self.field.field.required:
131 130
             classes.append('required')
132 131
         if not self.is_first:
133 132
             classes.append('inline')
134 133
         attrs = {'class': ' '.join(classes)} if classes else {}
135  
-        return self.field.label_tag(contents=mark_safe(contents), attrs=attrs)
  134
+        # checkboxes should not have a label suffix as the checkbox appears
  135
+        # to the left of the label.
  136
+        return self.field.label_tag(contents=mark_safe(contents), attrs=attrs,
  137
+                                    label_suffix='' if self.is_checkbox else None)
136 138
 
137 139
     def errors(self):
138 140
         return mark_safe(self.field.errors.as_ul())
9  django/forms/forms.py
@@ -509,20 +509,23 @@ def value(self):
509 509
             )
510 510
         return self.field.prepare_value(data)
511 511
 
512  
-    def label_tag(self, contents=None, attrs=None):
  512
+    def label_tag(self, contents=None, attrs=None, label_suffix=None):
513 513
         """
514 514
         Wraps the given contents in a <label>, if the field has an ID attribute.
515 515
         contents should be 'mark_safe'd to avoid HTML escaping. If contents
516 516
         aren't given, uses the field's HTML-escaped label.
517 517
 
518 518
         If attrs are given, they're used as HTML attributes on the <label> tag.
  519
+
  520
+        label_suffix allows overriding the form's label_suffix.
519 521
         """
520 522
         contents = contents or self.label
521 523
         # Only add the suffix if the label does not end in punctuation.
522 524
         # Translators: If found as last label character, these punctuation
523 525
         # characters will prevent the default label_suffix to be appended to the label
524  
-        if self.form.label_suffix and contents and contents[-1] not in _(':?.!'):
525  
-            contents = format_html('{0}{1}', contents, self.form.label_suffix)
  526
+        label_suffix = label_suffix if label_suffix is not None else self.form.label_suffix
  527
+        if label_suffix and contents and contents[-1] not in _(':?.!'):
  528
+            contents = format_html('{0}{1}', contents, label_suffix)
526 529
         widget = self.field.widget
527 530
         id_ = widget.attrs.get('id') or self.auto_id
528 531
         if id_:
15  docs/ref/forms/api.txt
@@ -527,6 +527,11 @@ Note that the label suffix is added only if the last character of the
527 527
 label isn't a punctuation character (in English, those are ``.``, ``!``, ``?``
528 528
 or ``:``).
529 529
 
  530
+.. versionadded:: 1.6
  531
+
  532
+You can also customize the ``label_suffix`` on a per-field basis using the
  533
+``label_suffix`` parameter to :meth:`~django.forms.BoundField.label_tag`.
  534
+
530 535
 Notes on field ordering
531 536
 ~~~~~~~~~~~~~~~~~~~~~~~
532 537
 
@@ -653,7 +658,7 @@ when printed::
653 658
     >>> str(f['subject'].errors)
654 659
     ''
655 660
 
656  
-.. method:: BoundField.label_tag(contents=None, attrs=None)
  661
+.. method:: BoundField.label_tag(contents=None, attrs=None, label_suffix=None)
657 662
 
658 663
 To separately render the label tag of a form field, you can call its
659 664
 ``label_tag`` method::
@@ -671,6 +676,14 @@ additional attributes for the ``<label>`` tag.
671 676
     The label now includes the form's :attr:`~django.forms.Form.label_suffix`
672 677
     (a colon, by default).
673 678
 
  679
+.. versionadded:: 1.6
  680
+
  681
+    The optional ``label_suffix`` parameter allows you to override the form's
  682
+    :attr:`~django.forms.Form.label_suffix`. For example, you can use an empty
  683
+    string to hide the label on selected fields. If you need to do this in a
  684
+    template, you could write a custom filter to allow passing parameters to
  685
+    ``label_tag``.
  686
+
674 687
 .. method:: BoundField.css_classes()
675 688
 
676 689
 When you use Django's rendering shortcuts, CSS classes are used to
4  docs/releases/1.6.txt
@@ -660,7 +660,9 @@ will render something like:
660 660
     <label for="id_my_field">My Field:</label> <input id="id_my_field" type="text" name="my_field" />
661 661
 
662 662
 If you want to keep the current behavior of rendering ``label_tag`` without
663  
-the ``label_suffix``, instantiate the form ``label_suffix=''``.
  663
+the ``label_suffix``, instantiate the form ``label_suffix=''``. You can also
  664
+customize the ``label_suffix`` on a per-field basis using the new
  665
+``label_suffix`` parameter on :meth:`~django.forms.BoundField.label_tag`.
664 666
 
665 667
 Admin views ``_changelist_filters`` GET parameter
666 668
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
4  tests/admin_util/tests.py
@@ -301,7 +301,7 @@ class MyForm(forms.Form):
301 301
         self.assertHTMLEqual(helpers.AdminField(form, 'text', is_first=False).label_tag(),
302 302
                              '<label for="id_text" class="required inline"><i>text</i>:</label>')
303 303
         self.assertHTMLEqual(helpers.AdminField(form, 'cb', is_first=False).label_tag(),
304  
-                             '<label for="id_cb" class="vCheckboxLabel required inline"><i>cb</i>:</label>')
  304
+                             '<label for="id_cb" class="vCheckboxLabel required inline"><i>cb</i></label>')
305 305
 
306 306
         # normal strings needs to be escaped
307 307
         class MyForm(forms.Form):
@@ -312,7 +312,7 @@ class MyForm(forms.Form):
312 312
         self.assertHTMLEqual(helpers.AdminField(form, 'text', is_first=False).label_tag(),
313 313
                              '<label for="id_text" class="required inline">&amp;text:</label>')
314 314
         self.assertHTMLEqual(helpers.AdminField(form, 'cb', is_first=False).label_tag(),
315  
-                             '<label for="id_cb" class="vCheckboxLabel required inline">&amp;cb:</label>')
  315
+                             '<label for="id_cb" class="vCheckboxLabel required inline">&amp;cb</label>')
316 316
 
317 317
     def test_flatten_fieldsets(self):
318 318
         """
10  tests/forms_tests/tests/test_forms.py
@@ -1870,3 +1870,13 @@ class SomeForm(Form):
1870 1870
         boundfield = SomeForm()['field']
1871 1871
 
1872 1872
         self.assertHTMLEqual(boundfield.label_tag(), '<label for="id_field"></label>')
  1873
+
  1874
+    def test_label_tag_override(self):
  1875
+        """
  1876
+        BoundField label_suffix (if provided) overrides Form label_suffix
  1877
+        """
  1878
+        class SomeForm(Form):
  1879
+            field = CharField()
  1880
+        boundfield = SomeForm(label_suffix='!')['field']
  1881
+
  1882
+        self.assertHTMLEqual(boundfield.label_tag(label_suffix='$'), '<label for="id_field">Field$</label>')

0 notes on commit dbbd2b1

Please sign in to comment.
Something went wrong with that request. Please try again.