1
0
mirror of https://github.com/django/django.git synced 2025-10-30 00:56:09 +00:00

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.
This commit is contained in:
Julen Ruiz Aizpuru
2014-04-29 10:07:57 +02:00
committed by Tim Graham
parent f2a8e47cfd
commit 5eb81ce445
6 changed files with 58 additions and 14 deletions

View File

@@ -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()

View File

@@ -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 _(':?.!'):