Author: mtredinnick
Date: 2007-09-15 23:38:20 -0500 (Sat, 15 Sep 2007)
New Revision: 6352

Modified:
   django/trunk/AUTHORS
   django/trunk/django/newforms/forms.py
   django/trunk/docs/newforms.txt
   django/trunk/tests/regressiontests/forms/tests.py
Log:
Fixed #4975 -- Allow the default label suffix character to be configured. 
Thanks, Vincent Foley.


Modified: django/trunk/AUTHORS
===================================================================
--- django/trunk/AUTHORS        2007-09-16 03:57:33 UTC (rev 6351)
+++ django/trunk/AUTHORS        2007-09-16 04:38:20 UTC (rev 6352)
@@ -122,6 +122,7 @@
     Afonso Fernández Nogueira <[EMAIL PROTECTED]>
     Matthew Flanagan <http://wadofstuff.blogspot.com>
     Eric Floehr <[EMAIL PROTECTED]>
+    Vincent Foley <[EMAIL PROTECTED]>
     Jorge Gajon <[EMAIL PROTECTED]>
     [EMAIL PROTECTED]
     Marc Garcia <[EMAIL PROTECTED]>

Modified: django/trunk/django/newforms/forms.py
===================================================================
--- django/trunk/django/newforms/forms.py       2007-09-16 03:57:33 UTC (rev 
6351)
+++ django/trunk/django/newforms/forms.py       2007-09-16 04:38:20 UTC (rev 
6352)
@@ -58,7 +58,7 @@
     # 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 @@
         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 @@
                     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 = ''

Modified: django/trunk/docs/newforms.txt
===================================================================
--- django/trunk/docs/newforms.txt      2007-09-16 03:57:33 UTC (rev 6351)
+++ django/trunk/docs/newforms.txt      2007-09-16 04:38:20 UTC (rev 6352)
@@ -513,6 +513,26 @@
 
 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()
+    <li><label for="id_for_subject">Subject</label> <input id="id_for_subject" 
type="text" name="subject" maxlength="100" /></li>
+    <li><label for="id_for_message">Message</label> <input type="text" 
name="message" id="id_for_message" /></li>
+    <li><label for="id_for_sender">Sender</label> <input type="text" 
name="sender" id="id_for_sender" /></li>
+    <li><label for="id_for_cc_myself">Cc myself</label> <input type="checkbox" 
name="cc_myself" id="id_for_cc_myself" /></li>
+    >>> f = ContactForm(auto_id='id_for_%s', label_suffix=' ->')
+    >>> print f.as_ul()
+    <li><label for="id_for_subject">Subject -></label> <input 
id="id_for_subject" type="text" name="subject" maxlength="100" /></li>
+    <li><label for="id_for_message">Message -></label> <input type="text" 
name="message" id="id_for_message" /></li>
+    <li><label for="id_for_sender">Sender -></label> <input type="text" 
name="sender" id="id_for_sender" /></li>
+    <li><label for="id_for_cc_myself">Cc myself -></label> <input 
type="checkbox" name="cc_myself" id="id_for_cc_myself" /></li>
+
+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
 ~~~~~~~~~~~~~~~~~~~~~~~
 

Modified: django/trunk/tests/regressiontests/forms/tests.py
===================================================================
--- django/trunk/tests/regressiontests/forms/tests.py   2007-09-16 03:57:33 UTC 
(rev 6351)
+++ django/trunk/tests/regressiontests/forms/tests.py   2007-09-16 04:38:20 UTC 
(rev 6352)
@@ -2943,6 +2943,37 @@
 <li><label for="id_username">Username:</label> <input id="id_username" 
type="text" name="username" maxlength="10" /></li>
 <li><label for="id_password">Password:</label> <input type="password" 
name="password" id="id_password" /></li>
 
+
+# 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()
+<li>Favorite color? <input type="text" name="color" /></li>
+<li>Favorite animal: <input type="text" name="animal" /></li>
+>>> f = FavoriteForm(auto_id=False, label_suffix='?')
+>>> print f.as_ul()
+<li>Favorite color? <input type="text" name="color" /></li>
+<li>Favorite animal? <input type="text" name="animal" /></li>
+>>> f = FavoriteForm(auto_id=False, label_suffix='')
+>>> print f.as_ul()
+<li>Favorite color? <input type="text" name="color" /></li>
+<li>Favorite animal <input type="text" name="animal" /></li>
+>>> f = FavoriteForm(auto_id=False, label_suffix=u'\u2192')
+>>> f.as_ul()
+u'<li>Favorite color? <input type="text" name="color" /></li>\n<li>Favorite 
animal\u2192 <input type="text" name="animal" /></li>'
+
+
+
 # Initial data ################################################################
 
 You can specify initial data for a field by using the 'initial' argument to a


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to