Django

Code

Changeset 6352

Show
Ignore:
Timestamp:
09/15/07 23:38:20 (1 year ago)
Author:
mtredinnick
Message:

Fixed #4975 -- Allow the default label suffix character to be configured. Thanks, Vincent Foley.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/AUTHORS

    r6350 r6352  
    123123    Matthew Flanagan <http://wadofstuff.blogspot.com> 
    124124    Eric Floehr <eric@intellovations.com> 
     125    Vincent Foley <vfoleybourgon@yahoo.ca> 
    125126    Jorge Gajon <gajon@gajon.org> 
    126127    gandalf@owca.info 
  • django/trunk/django/newforms/forms.py

    r6273 r6352  
    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 {} 
     
    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 
     
    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: 
  • django/trunk/docs/newforms.txt

    r6288 r6352  
    513513 
    514514By default, ``auto_id`` is set to the string ``'id_%s'``. 
     515 
     516Normally, a colon (``:``) will be appended after any label name when a form is 
     517rendered. It's possible to change the colon to another character, or omit it 
     518entirely, using 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 ``:``) 
    515535 
    516536Notes on field ordering 
  • django/trunk/tests/regressiontests/forms/tests.py

    r6282 r6352  
    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