From 5eb81ce44532596ecc1c781d93f3421a467a6206 Mon Sep 17 00:00:00 2001 From: Julen Ruiz Aizpuru Date: Tue, 29 Apr 2014 10:07:57 +0200 Subject: [PATCH] Fixed #22533 -- Added label_suffix parameter to form fields. Fields can now receive the `label_suffix` attribute, which will override a form's `label_suffix`. This enhances the possibility to customize form's `label_suffix`, allowing to use such customizations while using shortcuts such as `{{ form.as_p }}`. Note that the field's own customization can be overridden at runtime by using the `label_prefix` parameter to `BoundField.label_tag()`. Refs #18134. --- django/forms/fields.py | 5 ++++- django/forms/forms.py | 4 +++- docs/ref/forms/api.txt | 21 +++++++++++++-------- docs/ref/forms/fields.txt | 20 ++++++++++++++++++++ docs/releases/1.8.txt | 7 +++++++ tests/forms_tests/tests/test_forms.py | 15 +++++++++++---- 6 files changed, 58 insertions(+), 14 deletions(-) diff --git a/django/forms/fields.py b/django/forms/fields.py index 4fc5561c84..934192c037 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -61,7 +61,7 @@ class Field(object): def __init__(self, required=True, widget=None, label=None, initial=None, help_text='', error_messages=None, show_hidden_initial=False, - validators=[], localize=False): + validators=[], localize=False, label_suffix=None): # required -- Boolean that specifies whether the field is required. # True by default. # widget -- A Widget class, or instance of a Widget class, that should @@ -81,9 +81,12 @@ class Field(object): # hidden widget with initial value after widget. # validators -- List of addtional validators to use # localize -- Boolean that specifies if the field should be localized. + # label_suffix -- Suffix to be added to the label. Overrides + # form's label_suffix. self.required, self.label, self.initial = required, label, initial self.show_hidden_initial = show_hidden_initial self.help_text = help_text + self.label_suffix = label_suffix widget = widget or self.widget if isinstance(widget, type): widget = widget() diff --git a/django/forms/forms.py b/django/forms/forms.py index f248d08726..f868165977 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -616,8 +616,10 @@ class BoundField(object): label_suffix allows overriding the form's label_suffix. """ contents = contents or self.label + if label_suffix is None: + label_suffix = (self.field.label_suffix if self.field.label_suffix is not None + else self.form.label_suffix) # Only add the suffix if the label does not end in punctuation. - label_suffix = label_suffix if label_suffix is not None else self.form.label_suffix # Translators: If found as last label character, these punctuation # characters will prevent the default label_suffix to be appended to the label if label_suffix and contents and contents[-1] not in _(':?.!'): diff --git a/docs/ref/forms/api.txt b/docs/ref/forms/api.txt index 8a5303710a..950bd3a088 100644 --- a/docs/ref/forms/api.txt +++ b/docs/ref/forms/api.txt @@ -652,8 +652,13 @@ 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 ``:``). -You can also customize the ``label_suffix`` on a per-field basis using the -``label_suffix`` parameter to :meth:`~django.forms.BoundField.label_tag`. +.. versionadded:: 1.8 + +Fields can also define their own :attr:`~django.forms.Field.label_suffix`. +This will take precedence over :attr:`Form.label_suffix +`. The suffix can also be overridden at runtime +using the ``label_suffix`` parameter to +:meth:`~django.forms.BoundField.label_tag`. Notes on field ordering ~~~~~~~~~~~~~~~~~~~~~~~ @@ -799,12 +804,12 @@ auto-generated label tag. An optional ``attrs`` dictionary may contain additional attributes for the ``