Author: adrian
Date: 2006-12-26 17:33:20 -0600 (Tue, 26 Dec 2006)
New Revision: 4240

Modified:
  django/trunk/django/newforms/forms.py
  django/trunk/tests/regressiontests/forms/tests.py
Log:
newforms: A label can now be the empty string, in which case a label won't be 
displayed

Modified: django/trunk/django/newforms/forms.py
===================================================================
--- django/trunk/django/newforms/forms.py       2006-12-26 23:16:16 UTC (rev 
4239)
+++ django/trunk/django/newforms/forms.py       2006-12-26 23:33:20 UTC (rev 
4240)
@@ -99,7 +99,8 @@
            else:
                if errors_on_separate_row and bf_errors:
                    output.append(error_row % bf_errors)
-                output.append(normal_row % {'errors': bf_errors, 'label': 
bf.label_tag(escape(bf.label+':')), 'field': bf})
+                label = bf.label and bf.label_tag(escape(bf.label + ':')) or ''
+                output.append(normal_row % {'errors': bf_errors, 'label': 
label, 'field': bf})
        if top_errors:
            output.insert(0, error_row % top_errors)
        if hidden_fields: # Insert any hidden fields in the last row.
@@ -187,7 +188,10 @@
        self.field = field
        self.name = name
        self.html_name = form.add_prefix(name)
-        self.label = self.field.label or pretty_name(name)
+        if self.field.label is None:
+            self.label = pretty_name(name)
+        else:
+            self.label = self.field.label

    def __unicode__(self):
        "Renders this field as an HTML widget."

Modified: django/trunk/tests/regressiontests/forms/tests.py
===================================================================
--- django/trunk/tests/regressiontests/forms/tests.py   2006-12-26 23:16:16 UTC 
(rev 4239)
+++ django/trunk/tests/regressiontests/forms/tests.py   2006-12-26 23:33:20 UTC 
(rev 4240)
@@ -2080,6 +2080,33 @@
>>> p.as_ul()
u'<li>\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111: <input type="text" name="username" maxlength="10" 
/></li>\n<li>\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111: <input type="password" name="password" /></li>'

+If a label is set to the empty string for a field, that field won't get a 
label.
+>>> class UserRegistration(Form):
+...    username = CharField(max_length=10, label='')
+...    password = CharField(widget=PasswordInput)
+>>> p = UserRegistration(auto_id=False)
+>>> print p.as_ul()
+<li> <input type="text" name="username" maxlength="10" /></li>
+<li>Password: <input type="password" name="password" /></li>
+>>> p = UserRegistration(auto_id='id_%s')
+>>> print p.as_ul()
+<li> <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>
+
+If label is None, Django will auto-create the label from the field name. This
+is default behavior.
+>>> class UserRegistration(Form):
+...    username = CharField(max_length=10, label=None)
+...    password = CharField(widget=PasswordInput)
+>>> p = UserRegistration(auto_id=False)
+>>> print p.as_ul()
+<li>Username: <input type="text" name="username" maxlength="10" /></li>
+<li>Password: <input type="password" name="password" /></li>
+>>> p = UserRegistration(auto_id='id_%s')
+>>> print p.as_ul()
+<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>
+
# Forms with prefixes #########################################################

Sometimes it's necessary to have multiple forms display on the same HTML page,


--~--~---------~--~----~------------~-------~--~----~
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