Ticket #3064: newforms_label.patch
File newforms_label.patch, 4.2 KB (added by , 18 years ago) |
---|
-
django/newforms/forms.py
3 3 """ 4 4 5 5 from django.utils.datastructures import SortedDict 6 from django.utils.html import escape 6 7 from fields import Field 7 8 from widgets import TextInput, Textarea 8 9 from util import ErrorDict, ErrorList, ValidationError … … 190 191 "Returns a string of HTML for representing this as a <textarea>." 191 192 return self.as_widget(Textarea(), attrs) 192 193 194 def _verbose_name(self): 195 return pretty_name(self._name) 196 verbose_name = property(_verbose_name) 197 193 198 def _label(self): 194 return pretty_name(self._name) 199 """ 200 Returns the verbose field name (HTML escaped) and wraps with an HTML 201 label tag if either the field has an auto_id or the default widget has 202 an id attribute. 203 """ 204 label = escape(self.verbose_name) 205 id = self._field.widget.attrs.get('id') or self.auto_id 206 if id: 207 label = '<label for="%s">%s</label>' % (escape(id), label) 208 return label 195 209 label = property(_label) 196 210 197 211 def _auto_id(self): -
tests/regressiontests/forms/tests.py
1252 1252 "auto_id" tells the Form to add an "id" attribute to each form element. 1253 1253 If it's a string that contains '%s', Django will use that as a format string 1254 1254 into which the field's name will be inserted. 1255 Form elements with ids also get html label tags. 1255 1256 >>> p = Person(auto_id='id_%s') 1256 1257 >>> print p.as_ul() 1257 <li> First name: <input type="text" name="first_name" id="id_first_name" /></li>1258 <li> Last name: <input type="text" name="last_name" id="id_last_name" /></li>1259 <li> Birthday: <input type="text" name="birthday" id="id_birthday" /></li>1258 <li><label for="id_first_name">First name</label>: <input type="text" name="first_name" id="id_first_name" /></li> 1259 <li><label for="id_last_name">Last name</label>: <input type="text" name="last_name" id="id_last_name" /></li> 1260 <li><label for="id_birthday">Birthday</label>: <input type="text" name="birthday" id="id_birthday" /></li> 1260 1261 1261 1262 If auto_id is any True value whose str() does not contain '%s', the "id" 1262 1263 attribute will be the name of the field. 1263 1264 >>> p = Person(auto_id=True) 1264 1265 >>> print p.as_ul() 1265 <li> First name: <input type="text" name="first_name" id="first_name" /></li>1266 <li> Last name: <input type="text" name="last_name" id="last_name" /></li>1267 <li> Birthday: <input type="text" name="birthday" id="birthday" /></li>1266 <li><label for="first_name">First name</label>: <input type="text" name="first_name" id="first_name" /></li> 1267 <li><label for="last_name">Last name</label>: <input type="text" name="last_name" id="last_name" /></li> 1268 <li><label for="birthday">Birthday</label>: <input type="text" name="birthday" id="birthday" /></li> 1268 1269 1269 1270 If auto_id is any False value, an "id" attribute won't be output unless it 1270 1271 was manually entered. … … 1282 1283 ... birthday = DateField() 1283 1284 >>> p = PersonNew(auto_id=False) 1284 1285 >>> print p.as_ul() 1285 <li> First name: <input type="text" id="first_name_id" name="first_name" /></li>1286 <li><label for="first_name_id">First name</label>: <input type="text" id="first_name_id" name="first_name" /></li> 1286 1287 <li>Last name: <input type="text" name="last_name" /></li> 1287 1288 <li>Birthday: <input type="text" name="birthday" /></li> 1288 1289 … … 1290 1291 attribute in the Form gets precedence. 1291 1292 >>> p = PersonNew(auto_id=True) 1292 1293 >>> print p.as_ul() 1293 <li> First name: <input type="text" id="first_name_id" name="first_name" /></li>1294 <li> Last name: <input type="text" name="last_name" id="last_name" /></li>1295 <li> Birthday: <input type="text" name="birthday" id="birthday" /></li>1294 <li><label for="first_name_id">First name</label>: <input type="text" id="first_name_id" name="first_name" /></li> 1295 <li><label for="last_name">Last name</label>: <input type="text" name="last_name" id="last_name" /></li> 1296 <li><label for="birthday">Birthday</label>: <input type="text" name="birthday" id="birthday" /></li> 1296 1297 1297 1298 >>> class SignupForm(Form): 1298 1299 ... email = EmailField()