Ticket #4975: configurable_suffix.patch

File configurable_suffix.patch, 5.4 KB (added by gnuvince, 8 years ago)
  • django/newforms/forms.py

     
    5858    # information. Any improvements to the form API should be made to *this*
    5959    # class, not to the Form class.
    6060    def __init__(self, data=None, files=None, auto_id='id_%s', prefix=None,
    61             initial=None, error_class=ErrorList):
     61                 initial=None, error_class=ErrorList, label_suffix=':'):
    6262        self.is_bound = data is not None or files is not None
    6363        self.data = data or {}
    6464        self.files = files or {}
     
    6666        self.prefix = prefix
    6767        self.initial = initial or {}
    6868        self.error_class = error_class
     69        self.label_suffix = label_suffix
    6970        self._errors = None # Stores the errors after clean() has been called.
    7071
    7172        # The base_fields class attribute is the *class-wide* definition of
     
    129130                    output.append(error_row % force_unicode(bf_errors))
    130131                if bf.label:
    131132                    label = escape(force_unicode(bf.label))
    132                     # Only add a colon if the label does not end in punctuation.
    133                     if label[-1] not in ':?.!':
    134                         label += ':'
     133                    # Only add the suffix if the label does not end in punctuation.
     134                    if self.label_suffix:
     135                        if label[-1] not in ':?.!':
     136                            label += self.label_suffix
    135137                    label = bf.label_tag(label) or ''
    136138                else:
    137139                    label = ''
  • tests/regressiontests/forms/tests.py

     
    29432943<li><label for="id_username">Username:</label> <input id="id_username" type="text" name="username" maxlength="10" /></li>
    29442944<li><label for="id_password">Password:</label> <input type="password" name="password" id="id_password" /></li>
    29452945
     2946
     2947# Label Suffix ################################################################
     2948
     2949You can specify the 'label_suffix' argument to a Form class to modify the
     2950punctuation symbol used at the end of a label.  By default, the colon (:) is
     2951used, and is only appended to the label if the label doesn't already end with a
     2952punctuation symbol: ., !, ? or :.  If you specify a different suffix, it will
     2953be appended regardless of the last character of the label.
     2954
     2955>>> class FavoriteForm(Form):
     2956...     color = CharField(label='Favorite color?')
     2957...     animal = CharField(label='Favorite animal')
     2958...
     2959>>> f = FavoriteForm(auto_id=False)
     2960>>> print f.as_ul()
     2961<li>Favorite color? <input type="text" name="color" /></li>
     2962<li>Favorite animal: <input type="text" name="animal" /></li>
     2963>>> f = FavoriteForm(auto_id=False, label_suffix='?')
     2964>>> print f.as_ul()
     2965<li>Favorite color? <input type="text" name="color" /></li>
     2966<li>Favorite animal? <input type="text" name="animal" /></li>
     2967>>> f = FavoriteForm(auto_id=False, label_suffix='')
     2968>>> print f.as_ul()
     2969<li>Favorite color? <input type="text" name="color" /></li>
     2970<li>Favorite animal <input type="text" name="animal" /></li>
     2971>>> f = FavoriteForm(auto_id=False, label_suffix=u'\u2192')
     2972>>> f.as_ul()
     2973u'<li>Favorite color? <input type="text" name="color" /></li>\n<li>Favorite animal\u2192 <input type="text" name="animal" /></li>'
     2974
     2975
     2976
    29462977# Initial data ################################################################
    29472978
    29482979You can specify initial data for a field by using the 'initial' argument to a
  • docs/newforms.txt

     
    513513
    514514By default, ``auto_id`` is set to the string ``'id_%s'``.
    515515
     516By default, when using one of the as_* methods, a colon (:) will be
     517appended after the label name.  It is possible to change the colon to
     518another character by specifying the ``label_suffix`` parameter.
     519
     520    >>> f = ContactForm(auto_id='id_for_%s', label_suffix='')
     521    >>> print f.as_ul()
     522    <li><label for="id_for_subject">Subject</label> <input id="id_for_subject" type="text" name="subject" maxlength="100" /></li>
     523    <li><label for="id_for_message">Message</label> <input type="text" name="message" id="id_for_message" /></li>
     524    <li><label for="id_for_sender">Sender</label> <input type="text" name="sender" id="id_for_sender" /></li>
     525    <li><label for="id_for_cc_myself">Cc myself</label> <input type="checkbox" name="cc_myself" id="id_for_cc_myself" /></li>
     526    >>> f = ContactForm(auto_id='id_for_%s', label_suffix=' ->')
     527    >>> print f.as_ul()
     528    <li><label for="id_for_subject">Subject -></label> <input id="id_for_subject" type="text" name="subject" maxlength="100" /></li>
     529    <li><label for="id_for_message">Message -></label> <input type="text" name="message" id="id_for_message" /></li>
     530    <li><label for="id_for_sender">Sender -></label> <input type="text" name="sender" id="id_for_sender" /></li>
     531    <li><label for="id_for_cc_myself">Cc myself -></label> <input type="checkbox" name="cc_myself" id="id_for_cc_myself" /></li>
     532
     533Note that the label suffix is added only if the last character of the
     534label isn't a punctuation character (., !, ? or :)
     535
    516536Notes on field ordering
    517537~~~~~~~~~~~~~~~~~~~~~~~
    518538
Back to Top