Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

newforms: A label can now be the empty string, in which case a label …

…won't be displayed

git-svn-id: http://code.djangoproject.com/svn/django/trunk@4240 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d0fcef9db0f53ef84535694f4fbcb135f0648e24 1 parent 30c2bff
Adrian Holovaty authored December 26, 2006
8  django/newforms/forms.py
@@ -99,7 +99,8 @@ def _html_output(self, normal_row, error_row, row_ender, errors_on_separate_row)
99 99
             else:
100 100
                 if errors_on_separate_row and bf_errors:
101 101
                     output.append(error_row % bf_errors)
102  
-                output.append(normal_row % {'errors': bf_errors, 'label': bf.label_tag(escape(bf.label+':')), 'field': bf})
  102
+                label = bf.label and bf.label_tag(escape(bf.label + ':')) or ''
  103
+                output.append(normal_row % {'errors': bf_errors, 'label': label, 'field': bf})
103 104
         if top_errors:
104 105
             output.insert(0, error_row % top_errors)
105 106
         if hidden_fields: # Insert any hidden fields in the last row.
@@ -187,7 +188,10 @@ def __init__(self, form, field, name):
187 188
         self.field = field
188 189
         self.name = name
189 190
         self.html_name = form.add_prefix(name)
190  
-        self.label = self.field.label or pretty_name(name)
  191
+        if self.field.label is None:
  192
+            self.label = pretty_name(name)
  193
+        else:
  194
+            self.label = self.field.label
191 195
 
192 196
     def __unicode__(self):
193 197
         "Renders this field as an HTML widget."
27  tests/regressiontests/forms/tests.py
@@ -2080,6 +2080,33 @@
2080 2080
 >>> p.as_ul()
2081 2081
 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>'
2082 2082
 
  2083
+If a label is set to the empty string for a field, that field won't get a label.
  2084
+>>> class UserRegistration(Form):
  2085
+...    username = CharField(max_length=10, label='')
  2086
+...    password = CharField(widget=PasswordInput)
  2087
+>>> p = UserRegistration(auto_id=False)
  2088
+>>> print p.as_ul()
  2089
+<li> <input type="text" name="username" maxlength="10" /></li>
  2090
+<li>Password: <input type="password" name="password" /></li>
  2091
+>>> p = UserRegistration(auto_id='id_%s')
  2092
+>>> print p.as_ul()
  2093
+<li> <input id="id_username" type="text" name="username" maxlength="10" /></li>
  2094
+<li><label for="id_password">Password:</label> <input type="password" name="password" id="id_password" /></li>
  2095
+
  2096
+If label is None, Django will auto-create the label from the field name. This
  2097
+is default behavior.
  2098
+>>> class UserRegistration(Form):
  2099
+...    username = CharField(max_length=10, label=None)
  2100
+...    password = CharField(widget=PasswordInput)
  2101
+>>> p = UserRegistration(auto_id=False)
  2102
+>>> print p.as_ul()
  2103
+<li>Username: <input type="text" name="username" maxlength="10" /></li>
  2104
+<li>Password: <input type="password" name="password" /></li>
  2105
+>>> p = UserRegistration(auto_id='id_%s')
  2106
+>>> print p.as_ul()
  2107
+<li><label for="id_username">Username:</label> <input id="id_username" type="text" name="username" maxlength="10" /></li>
  2108
+<li><label for="id_password">Password:</label> <input type="password" name="password" id="id_password" /></li>
  2109
+
2083 2110
 # Forms with prefixes #########################################################
2084 2111
 
2085 2112
 Sometimes it's necessary to have multiple forms display on the same HTML page,

0 notes on commit d0fcef9

Please sign in to comment.
Something went wrong with that request. Please try again.