Ticket #4975: configurable_suffix.patch
File configurable_suffix.patch, 5.4 KB (added by , 17 years ago) |
---|
-
django/newforms/forms.py
58 58 # information. Any improvements to the form API should be made to *this* 59 59 # class, not to the Form class. 60 60 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=':'): 62 62 self.is_bound = data is not None or files is not None 63 63 self.data = data or {} 64 64 self.files = files or {} … … 66 66 self.prefix = prefix 67 67 self.initial = initial or {} 68 68 self.error_class = error_class 69 self.label_suffix = label_suffix 69 70 self._errors = None # Stores the errors after clean() has been called. 70 71 71 72 # The base_fields class attribute is the *class-wide* definition of … … 129 130 output.append(error_row % force_unicode(bf_errors)) 130 131 if bf.label: 131 132 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 135 137 label = bf.label_tag(label) or '' 136 138 else: 137 139 label = '' -
tests/regressiontests/forms/tests.py
2943 2943 <li><label for="id_username">Username:</label> <input id="id_username" type="text" name="username" maxlength="10" /></li> 2944 2944 <li><label for="id_password">Password:</label> <input type="password" name="password" id="id_password" /></li> 2945 2945 2946 2947 # Label Suffix ################################################################ 2948 2949 You can specify the 'label_suffix' argument to a Form class to modify the 2950 punctuation symbol used at the end of a label. By default, the colon (:) is 2951 used, and is only appended to the label if the label doesn't already end with a 2952 punctuation symbol: ., !, ? or :. If you specify a different suffix, it will 2953 be 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() 2973 u'<li>Favorite color? <input type="text" name="color" /></li>\n<li>Favorite animal\u2192 <input type="text" name="animal" /></li>' 2974 2975 2976 2946 2977 # Initial data ################################################################ 2947 2978 2948 2979 You can specify initial data for a field by using the 'initial' argument to a -
docs/newforms.txt
513 513 514 514 By default, ``auto_id`` is set to the string ``'id_%s'``. 515 515 516 By default, when using one of the as_* methods, a colon (:) will be 517 appended after the label name. It is possible to change the colon to 518 another 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 533 Note that the label suffix is added only if the last character of the 534 label isn't a punctuation character (., !, ? or :) 535 516 536 Notes on field ordering 517 537 ~~~~~~~~~~~~~~~~~~~~~~~ 518 538