diff options
| author | Tim Graham <timograham@gmail.com> | 2013-07-26 14:43:46 -0400 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2013-07-26 14:45:38 -0400 |
| commit | 8676318d2dae9a570d2314e4e6da8c00aaf2e2a0 (patch) | |
| tree | 86e3bd1bd11dd09d9aaf288357f9feacffb3d363 | |
| parent | 2a979d2a7bec485e4b90b7ae99ace0dd16faa948 (diff) | |
Fixed #20805 -- Removed an extra colon beside checkboxes in the admin.
Thanks CollinAnderson for the report.
| -rw-r--r-- | django/contrib/admin/helpers.py | 8 | ||||
| -rw-r--r-- | django/forms/forms.py | 9 | ||||
| -rw-r--r-- | docs/ref/forms/api.txt | 15 | ||||
| -rw-r--r-- | docs/releases/1.6.txt | 4 | ||||
| -rw-r--r-- | tests/admin_util/tests.py | 4 | ||||
| -rw-r--r-- | tests/forms_tests/tests/test_forms.py | 10 |
6 files changed, 40 insertions, 10 deletions
diff --git a/django/contrib/admin/helpers.py b/django/contrib/admin/helpers.py index 3ffb85e6c6..b6d5bde932 100644 --- a/django/contrib/admin/helpers.py +++ b/django/contrib/admin/helpers.py @@ -125,14 +125,16 @@ class AdminField(object): contents = conditional_escape(force_text(self.field.label)) if self.is_checkbox: classes.append('vCheckboxLabel') - else: - contents += ':' + if self.field.field.required: classes.append('required') if not self.is_first: classes.append('inline') attrs = {'class': ' '.join(classes)} if classes else {} - return self.field.label_tag(contents=mark_safe(contents), attrs=attrs) + # checkboxes should not have a label suffix as the checkbox appears + # to the left of the label. + return self.field.label_tag(contents=mark_safe(contents), attrs=attrs, + label_suffix='' if self.is_checkbox else None) def errors(self): return mark_safe(self.field.errors.as_ul()) diff --git a/django/forms/forms.py b/django/forms/forms.py index e144eb60f8..ad5daf4416 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -509,20 +509,23 @@ class BoundField(object): ) return self.field.prepare_value(data) - def label_tag(self, contents=None, attrs=None): + def label_tag(self, contents=None, attrs=None, label_suffix=None): """ Wraps 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 aren't given, uses the field's HTML-escaped label. If attrs are given, they're used as HTML attributes on the <label> tag. + + label_suffix allows overriding the form's label_suffix. """ contents = contents or self.label # Only add the suffix if the label does not end in punctuation. # Translators: If found as last label character, these punctuation # characters will prevent the default label_suffix to be appended to the label - if self.form.label_suffix and contents and contents[-1] not in _(':?.!'): - contents = format_html('{0}{1}', contents, self.form.label_suffix) + label_suffix = label_suffix if label_suffix is not None else self.form.label_suffix + if label_suffix and contents and contents[-1] not in _(':?.!'): + contents = format_html('{0}{1}', contents, label_suffix) widget = self.field.widget id_ = widget.attrs.get('id') or self.auto_id if id_: diff --git a/docs/ref/forms/api.txt b/docs/ref/forms/api.txt index 7c1601d3ea..780cb5d4f7 100644 --- a/docs/ref/forms/api.txt +++ b/docs/ref/forms/api.txt @@ -527,6 +527,11 @@ Note that the label suffix is added only if the last character of the label isn't a punctuation character (in English, those are ``.``, ``!``, ``?`` or ``:``). +.. versionadded:: 1.6 + +You can also customize the ``label_suffix`` on a per-field basis using the +``label_suffix`` parameter to :meth:`~django.forms.BoundField.label_tag`. + Notes on field ordering ~~~~~~~~~~~~~~~~~~~~~~~ @@ -653,7 +658,7 @@ when printed:: >>> str(f['subject'].errors) '' -.. method:: BoundField.label_tag(contents=None, attrs=None) +.. method:: BoundField.label_tag(contents=None, attrs=None, label_suffix=None) To separately render the label tag of a form field, you can call its ``label_tag`` method:: @@ -671,6 +676,14 @@ additional attributes for the ``<label>`` tag. The label now includes the form's :attr:`~django.forms.Form.label_suffix` (a colon, by default). +.. versionadded:: 1.6 + + The optional ``label_suffix`` parameter allows you to override the form's + :attr:`~django.forms.Form.label_suffix`. For example, you can use an empty + string to hide the label on selected fields. If you need to do this in a + template, you could write a custom filter to allow passing parameters to + ``label_tag``. + .. method:: BoundField.css_classes() When you use Django's rendering shortcuts, CSS classes are used to diff --git a/docs/releases/1.6.txt b/docs/releases/1.6.txt index 73b48edc85..355b107046 100644 --- a/docs/releases/1.6.txt +++ b/docs/releases/1.6.txt @@ -664,7 +664,9 @@ will render something like: <label for="id_my_field">My Field:</label> <input id="id_my_field" type="text" name="my_field" /> If you want to keep the current behavior of rendering ``label_tag`` without -the ``label_suffix``, instantiate the form ``label_suffix=''``. +the ``label_suffix``, instantiate the form ``label_suffix=''``. You can also +customize the ``label_suffix`` on a per-field basis using the new +``label_suffix`` parameter on :meth:`~django.forms.BoundField.label_tag`. Admin views ``_changelist_filters`` GET parameter ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/admin_util/tests.py b/tests/admin_util/tests.py index 637f643261..4a9a203f50 100644 --- a/tests/admin_util/tests.py +++ b/tests/admin_util/tests.py @@ -301,7 +301,7 @@ class UtilTests(SimpleTestCase): self.assertHTMLEqual(helpers.AdminField(form, 'text', is_first=False).label_tag(), '<label for="id_text" class="required inline"><i>text</i>:</label>') self.assertHTMLEqual(helpers.AdminField(form, 'cb', is_first=False).label_tag(), - '<label for="id_cb" class="vCheckboxLabel required inline"><i>cb</i>:</label>') + '<label for="id_cb" class="vCheckboxLabel required inline"><i>cb</i></label>') # normal strings needs to be escaped class MyForm(forms.Form): @@ -312,7 +312,7 @@ class UtilTests(SimpleTestCase): self.assertHTMLEqual(helpers.AdminField(form, 'text', is_first=False).label_tag(), '<label for="id_text" class="required inline">&text:</label>') self.assertHTMLEqual(helpers.AdminField(form, 'cb', is_first=False).label_tag(), - '<label for="id_cb" class="vCheckboxLabel required inline">&cb:</label>') + '<label for="id_cb" class="vCheckboxLabel required inline">&cb</label>') def test_flatten_fieldsets(self): """ diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py index 633fde5026..c77181273c 100644 --- a/tests/forms_tests/tests/test_forms.py +++ b/tests/forms_tests/tests/test_forms.py @@ -1870,3 +1870,13 @@ class FormsTestCase(TestCase): boundfield = SomeForm()['field'] self.assertHTMLEqual(boundfield.label_tag(), '<label for="id_field"></label>') + + def test_label_tag_override(self): + """ + BoundField label_suffix (if provided) overrides Form label_suffix + """ + class SomeForm(Form): + field = CharField() + boundfield = SomeForm(label_suffix='!')['field'] + + self.assertHTMLEqual(boundfield.label_tag(label_suffix='$'), '<label for="id_field">Field$</label>') |
