Ticket #20805: 20805.2.diff

File 20805.2.diff, 6.8 KB (added by Tim Graham, 11 years ago)
  • django/contrib/admin/helpers.py

    diff --git a/django/contrib/admin/helpers.py b/django/contrib/admin/helpers.py
    index 3ffb85e..b6d5bde 100644
    a b class AdminField(object):  
    125125        contents = conditional_escape(force_text(self.field.label))
    126126        if self.is_checkbox:
    127127            classes.append('vCheckboxLabel')
    128         else:
    129             contents += ':'
     128
    130129        if self.field.field.required:
    131130            classes.append('required')
    132131        if not self.is_first:
    133132            classes.append('inline')
    134133        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)
    136138
    137139    def errors(self):
    138140        return mark_safe(self.field.errors.as_ul())
  • django/forms/forms.py

    diff --git a/django/forms/forms.py b/django/forms/forms.py
    index e144eb6..ad5daf4 100644
    a b class BoundField(object):  
    509509            )
    510510        return self.field.prepare_value(data)
    511511
    512     def label_tag(self, contents=None, attrs=None):
     512    def label_tag(self, contents=None, attrs=None, label_suffix=None):
    513513        """
    514514        Wraps the given contents in a <label>, if the field has an ID attribute.
    515515        contents should be 'mark_safe'd to avoid HTML escaping. If contents
    516516        aren't given, uses the field's HTML-escaped label.
    517517
    518518        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.
    519521        """
    520522        contents = contents or self.label
    521523        # Only add the suffix if the label does not end in punctuation.
    522524        # Translators: If found as last label character, these punctuation
    523525        # 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)
    526529        widget = self.field.widget
    527530        id_ = widget.attrs.get('id') or self.auto_id
    528531        if id_:
  • docs/ref/forms/api.txt

    diff --git a/docs/ref/forms/api.txt b/docs/ref/forms/api.txt
    index 7c1601d..46a9c39 100644
    a b Note that the label suffix is added only if the last character of the  
    527527label isn't a punctuation character (in English, those are ``.``, ``!``, ``?``
    528528or ``:``).
    529529
     530.. versionadded:: 1.6
     531
     532You 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
    530535Notes on field ordering
    531536~~~~~~~~~~~~~~~~~~~~~~~
    532537
    when printed::  
    653658    >>> str(f['subject'].errors)
    654659    ''
    655660
    656 .. method:: BoundField.label_tag(contents=None, attrs=None)
     661.. method:: BoundField.label_tag(contents=None, attrs=None, label_suffix=None)
    657662
    658663To separately render the label tag of a form field, you can call its
    659664``label_tag`` method::
    additional attributes for the ``<label>`` tag.  
    671676    The label now includes the form's :attr:`~django.forms.Form.label_suffix`
    672677    (a colon, by default).
    673678
     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.
     684
    674685.. method:: BoundField.css_classes()
    675686
    676687When you use Django's rendering shortcuts, CSS classes are used to
  • docs/releases/1.6.txt

    diff --git a/docs/releases/1.6.txt b/docs/releases/1.6.txt
    index 73b48ed..355b107 100644
    a b will render something like:  
    664664    <label for="id_my_field">My Field:</label> <input id="id_my_field" type="text" name="my_field" />
    665665
    666666If you want to keep the current behavior of rendering ``label_tag`` without
    667 the ``label_suffix``, instantiate the form ``label_suffix=''``.
     667the ``label_suffix``, instantiate the form ``label_suffix=''``. You can also
     668customize the ``label_suffix`` on a per-field basis using the new
     669``label_suffix`` parameter on :meth:`~django.forms.BoundField.label_tag`.
    668670
    669671Admin views ``_changelist_filters`` GET parameter
    670672~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  • tests/admin_util/tests.py

    diff --git a/tests/admin_util/tests.py b/tests/admin_util/tests.py
    index 637f643..4a9a203 100644
    a b class UtilTests(SimpleTestCase):  
    301301        self.assertHTMLEqual(helpers.AdminField(form, 'text', is_first=False).label_tag(),
    302302                             '<label for="id_text" class="required inline"><i>text</i>:</label>')
    303303        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>')
    305305
    306306        # normal strings needs to be escaped
    307307        class MyForm(forms.Form):
    class UtilTests(SimpleTestCase):  
    312312        self.assertHTMLEqual(helpers.AdminField(form, 'text', is_first=False).label_tag(),
    313313                             '<label for="id_text" class="required inline">&amp;text:</label>')
    314314        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>')
    316316
    317317    def test_flatten_fieldsets(self):
    318318        """
  • tests/forms_tests/tests/test_forms.py

    diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py
    index 633fde5..c771812 100644
    a b class FormsTestCase(TestCase):  
    18701870        boundfield = SomeForm()['field']
    18711871
    18721872        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>')
Back to Top