Ticket #10427: django-forms-value.6.diff
File django-forms-value.6.diff, 4.4 KB (added by , 16 years ago) |
---|
-
django/forms/forms.py
366 366 auto_id = self.auto_id 367 367 if auto_id and 'id' not in attrs and 'id' not in widget.attrs: 368 368 attrs['id'] = auto_id 369 if not self.form.is_bound:370 data = self.form.initial.get(self.name, self.field.initial)371 if callable(data):372 data = data()373 else:374 data = self.data375 369 if not only_initial: 376 370 name = self.html_name 377 371 else: 378 372 name = self.html_initial_name 379 return widget.render(name, data, attrs=attrs)373 return widget.render(name, self.value, attrs=attrs) 380 374 381 375 def as_text(self, attrs=None, **kwargs): 382 376 """ … … 401 395 return self.field.widget.value_from_datadict(self.form.data, self.form.files, self.html_name) 402 396 data = property(_data) 403 397 398 def _value(self): 399 """ 400 Returns the value for this BoundField, as rendered in widgets. 401 """ 402 if not self.form.is_bound: 403 val = self.form.initial.get(self.name, self.field.initial) 404 if callable(val): 405 val = val() 406 else: 407 val = self.data 408 if val is None: 409 val = '' 410 return val 411 value = property(_value) 412 404 413 def label_tag(self, contents=None, attrs=None): 405 414 """ 406 415 Wraps the given contents in a <label>, if the field has an ID attribute. -
tests/regressiontests/forms/forms.py
1196 1196 <li>Username: <input type="text" name="username" value="stephane" maxlength="10" /></li> 1197 1197 <li>Password: <input type="password" name="password" /></li> 1198 1198 1199 # Bound field values ########################################################## 1200 1201 It's possible to get to the value which would be used for rendering the widget 1202 for a field by using the BoundField's value attribute. 1203 1204 >>> class UserRegistration(Form): 1205 ... username = CharField(max_length=10, initial='djangonaut') 1206 ... password = CharField(widget=PasswordInput) 1207 >>> p = UserRegistration({'password': 'foo'}) 1208 >>> print 'username.value =', p['username'].value 1209 username.value = 1210 >>> print 'username.data = ', p['username'].data 1211 username.data = None 1212 >>> print 'password.value =', p['password'].value 1213 password.value = foo 1214 >>> print 'password.data =', p['password'].data 1215 password.data = foo 1216 1217 The value of username is empty because the form is bound -- the value wasn't 1218 specified, and so is empty. This differs if the form were to be unbound: 1219 1220 >>> p = UserRegistration() 1221 >>> print p['username'].value 1222 djangonaut 1223 1199 1224 # Help text ################################################################### 1200 1225 1201 1226 You can specify descriptive text for a field by using the 'help_text' argument -
docs/ref/forms/fields.txt
293 293 <tr><th>Url:</th><td><input type="text" name="url" /></td></tr> 294 294 <tr><th>Comment:</th><td><input type="text" name="comment" /></td></tr> 295 295 296 Field data versus value 297 ~~~~~~~~~~~~~~~~~~~~~~~ 296 298 299 .. versionadded:: 1.1 300 301 .. attribute:: Field.value 302 .. attribute:: Field.data 303 304 Sometimes, it's necessary -- or at least easier -- to render the form entirely 305 on your own. In such a case, you might want to get to what Django's form 306 rendering would use for the HTML `value` attribute. 307 308 It's quite easy, actually:: 309 310 >>> f = CommentForm({'comment': 'Foobar'}, initial={'name': 'instance'}) 311 >>> f['name'].value 312 'instance' 313 >>> f['comment'].value 314 'Foobar' 315 316 The value attribute takes initial data into account on form level (i.e., 317 passing it to the initializer as seen in previous section), as well as field 318 level. 319 320 The resolution process is, step by step, the following: 321 322 1. If the form has data for this field, use it; 323 2. if the form has initial data for this field, use it; 324 3. if the field has initial data, use it; and lastly 325 4. use the empty string. 326 297 327 Built-in ``Field`` classes 298 328 -------------------------- 299 329