diff --git a/AUTHORS b/AUTHORS index 94659c88fa1..f382eb6b55c 100644 --- a/AUTHORS +++ b/AUTHORS @@ -122,6 +122,7 @@ answer newbie questions, and generally made Django that much better: Afonso Fernández Nogueira Matthew Flanagan Eric Floehr + Vincent Foley Jorge Gajon gandalf@owca.info Marc Garcia diff --git a/django/newforms/forms.py b/django/newforms/forms.py index 2b1caddedab..3196db339e2 100644 --- a/django/newforms/forms.py +++ b/django/newforms/forms.py @@ -58,7 +58,7 @@ class BaseForm(StrAndUnicode): # information. Any improvements to the form API should be made to *this* # class, not to the Form class. def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None, - initial=None, error_class=ErrorList): + initial=None, error_class=ErrorList, label_suffix=':'): self.is_bound = data is not None or files is not None self.data = data or {} self.files = files or {} @@ -66,6 +66,7 @@ class BaseForm(StrAndUnicode): self.prefix = prefix self.initial = initial or {} self.error_class = error_class + self.label_suffix = label_suffix self._errors = None # Stores the errors after clean() has been called. # The base_fields class attribute is the *class-wide* definition of @@ -129,9 +130,10 @@ class BaseForm(StrAndUnicode): output.append(error_row % force_unicode(bf_errors)) if bf.label: label = escape(force_unicode(bf.label)) - # Only add a colon if the label does not end in punctuation. - if label[-1] not in ':?.!': - label += ':' + # Only add the suffix if the label does not end in punctuation. + if self.label_suffix: + if label[-1] not in ':?.!': + label += self.label_suffix label = bf.label_tag(label) or '' else: label = '' diff --git a/docs/newforms.txt b/docs/newforms.txt index 19c5fc247f4..691aee7e444 100644 --- a/docs/newforms.txt +++ b/docs/newforms.txt @@ -513,6 +513,26 @@ include ``%s`` -- then the library will act as if ``auto_id`` is ``True``. By default, ``auto_id`` is set to the string ``'id_%s'``. +Normally, a colon (``:``) will be appended after any label name when a form is +rendered. It's possible to change the colon to another character, or omit it +entirely, using the ``label_suffix`` parameter:: + + >>> f = ContactForm(auto_id='id_for_%s', label_suffix='') + >>> print f.as_ul() +
  • +
  • +
  • +
  • + >>> f = ContactForm(auto_id='id_for_%s', label_suffix=' ->') + >>> print f.as_ul() +
  • +
  • +
  • +
  • + +Note that the label suffix is added only if the last character of the +label isn't a punctuation character (``.``, ``!``, ``?`` or ``:``) + Notes on field ordering ~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/tests/regressiontests/forms/tests.py b/tests/regressiontests/forms/tests.py index 72033a4e607..a3e61c8f064 100644 --- a/tests/regressiontests/forms/tests.py +++ b/tests/regressiontests/forms/tests.py @@ -2943,6 +2943,37 @@ is default behavior.
  • + +# Label Suffix ################################################################ + +You can specify the 'label_suffix' argument to a Form class to modify the +punctuation symbol used at the end of a label. By default, the colon (:) is +used, and is only appended to the label if the label doesn't already end with a +punctuation symbol: ., !, ? or :. If you specify a different suffix, it will +be appended regardless of the last character of the label. + +>>> class FavoriteForm(Form): +... color = CharField(label='Favorite color?') +... animal = CharField(label='Favorite animal') +... +>>> f = FavoriteForm(auto_id=False) +>>> print f.as_ul() +
  • Favorite color?
  • +
  • Favorite animal:
  • +>>> f = FavoriteForm(auto_id=False, label_suffix='?') +>>> print f.as_ul() +
  • Favorite color?
  • +
  • Favorite animal?
  • +>>> f = FavoriteForm(auto_id=False, label_suffix='') +>>> print f.as_ul() +
  • Favorite color?
  • +
  • Favorite animal
  • +>>> f = FavoriteForm(auto_id=False, label_suffix=u'\u2192') +>>> f.as_ul() +u'
  • Favorite color?
  • \n
  • Favorite animal\u2192
  • ' + + + # Initial data ################################################################ You can specify initial data for a field by using the 'initial' argument to a