Ticket #10427: django-forms-value.diff
File django-forms-value.diff, 4.7 KB (added by , 16 years ago) |
---|
-
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
257 257 In the `built-in Field classes`_ section below, each ``Field`` defines the 258 258 error message keys it uses. 259 259 260 Field data versus value 261 ~~~~~~~~~~~~~~~~~~~~~~~ 262 263 .. versionadded:: 1.1 264 265 .. attribute:: Field.value 266 .. attribute:: Field.data 267 268 Sometimes, it's necessary -- or at least easier -- to render the form entirely 269 on your own. In such a case, you might want to get to what Django's form 270 rendering would use for the HTML `value` attribute. 271 272 It's quite easy, actually:: 273 274 >>> f = CommentForm({'comment': 'Foobar'}, initial={'name': 'instance'}) 275 >>> f['name'].value 276 'instance' 277 >>> f['comment'].value 278 'Foobar' 279 280 The value attribute takes initial data into account on form level (i.e., 281 passing it to the initializer as seen in previous section), as well as field 282 level. 283 284 The resolution process is, step by step, the following: 285 286 1. If the form has data for this field, use it; 287 2. if the form has initial data for this field, use it; 288 3. if the field has initial data, use it; and lastly 289 4. use the empty string. 290 260 291 Built-in ``Field`` classes 261 292 -------------------------- 262 293 -
django/forms/forms.py
375 375 auto_id = self.auto_id 376 376 if auto_id and 'id' not in attrs and 'id' not in widget.attrs: 377 377 attrs['id'] = auto_id 378 if not self.form.is_bound:379 data = self.form.initial.get(self.name, self.field.initial)380 if callable(data):381 data = data()382 else:383 if isinstance(self.field, FileField) and self.data is None:384 data = self.form.initial.get(self.name, self.field.initial)385 else:386 data = self.data387 378 if not only_initial: 388 379 name = self.html_name 389 380 else: 390 381 name = self.html_initial_name 391 return widget.render(name, data, attrs=attrs)382 return widget.render(name, self.value, attrs=attrs) 392 383 393 384 def as_text(self, attrs=None, **kwargs): 394 385 """ … … 413 404 return self.field.widget.value_from_datadict(self.form.data, self.form.files, self.html_name) 414 405 data = property(_data) 415 406 407 def _value(self): 408 """ 409 Returns the value for this BoundField, as rendered in widgets. 410 """ 411 if not self.form.is_bound: 412 val = self.form.initial.get(self.name, self.field.initial) 413 if callable(val): 414 val = val() 415 else: 416 if isinstance(self.field, FileField) and self.data is None: 417 val = self.form.initial.get(self.name, self.field.initial) 418 else: 419 val = self.data 420 if val is None: 421 val = '' 422 return val 423 value = property(_value) 424 416 425 def label_tag(self, contents=None, attrs=None): 417 426 """ 418 427 Wraps the given contents in a <label>, if the field has an ID attribute.