Ticket #10427: django-display_value-r11298.patch

File django-display_value-r11298.patch, 6.5 KB (added by pdonis, 15 years ago)

adds one-line change to formtools/preview.html template

  • django/forms/forms.py

     
    99from django.utils.encoding import StrAndUnicode, smart_unicode, force_unicode
    1010from django.utils.safestring import mark_safe
    1111
    12 from fields import Field, FileField
     12from fields import Field, FileField, ChoiceField
    1313from widgets import Media, media_property, TextInput, Textarea
    1414from util import flatatt, ErrorDict, ErrorList, ValidationError
    1515
     
    375375        auto_id = self.auto_id
    376376        if auto_id and 'id' not in attrs and 'id' not in widget.attrs:
    377377            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.data
    387378        if not only_initial:
    388379            name = self.html_name
    389380        else:
    390381            name = self.html_initial_name
    391         return widget.render(name, data, attrs=attrs)
     382        return widget.render(name, self.value, attrs=attrs)
    392383
    393384    def as_text(self, attrs=None, **kwargs):
    394385        """
     
    413404        return self.field.widget.value_from_datadict(self.form.data, self.form.files, self.html_name)
    414405    data = property(_data)
    415406
     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
     425    def _display_value(self):
     426        """
     427        Returns the displayed value for this BoundField, as rendered in widgets.
     428        """
     429        value = self.value
     430        if isinstance(self.field, ChoiceField):
     431            for (val, desc) in self.field.choices:
     432                if val == value:
     433                    return desc
     434        return self.value
     435    display_value = property(_display_value)
     436
    416437    def label_tag(self, contents=None, attrs=None):
    417438        """
    418439        Wraps the given contents in a <label>, if the field has an ID attribute.
  • django/contrib/formtools/templates/formtools/preview.html

     
    88{% for field in form %}
    99<tr>
    1010<th>{{ field.label }}:</th>
    11 <td>{{ field.data }}</td>
     11<td>{{ field.display_value }}</td>
    1212</tr>
    1313{% endfor %}
    1414</table>
  • tests/regressiontests/forms/forms.py

     
    12341234<option value="w">whiz</option>
    12351235</select></li>
    12361236
     1237# Bound field values ##########################################################
     1238
     1239It's possible to get to the value which would be used for rendering the widget
     1240for a field by using the BoundField's value attribute.
     1241
     1242>>> class UserRegistration(Form):
     1243...    username = CharField(max_length=10, initial='djangonaut')
     1244...    password = CharField(widget=PasswordInput)
     1245...    gender = ChoiceField(choices=(('M', 'Male'), ('F', 'Female')))
     1246...    role = ChoiceField(choices=(('user', 'Site user'), ('admin', 'Site admin')), initial='user')
     1247>>> p = UserRegistration({'password': 'foo', 'gender': 'M'})
     1248>>> print 'username.value =', p['username'].value
     1249username.value =
     1250>>> print 'username.display_value =', p['username'].display_value
     1251username.display_value =
     1252>>> print 'username.data = ', p['username'].data
     1253username.data = None
     1254>>> print 'password.value =', p['password'].value
     1255password.value = foo
     1256>>> print 'password.display_value =', p['password'].display_value
     1257password.display_value = foo
     1258>>> print 'password.data =', p['password'].data
     1259password.data = foo
     1260>>> print 'gender.value =', p['gender'].value
     1261gender.value = M
     1262>>> print 'gender.display_value =', p['gender'].display_value
     1263gender.display_value = Male
     1264>>> print 'gender.data =', p['gender'].data
     1265gender.data = M
     1266>>> print 'role.value =', p['role'].value
     1267role.value =
     1268>>> print 'role.display_value =', p['role'].display_value
     1269role.display_value =
     1270>>> print 'role.data =', p['role'].data
     1271role.data = None
     1272
     1273The value of username is empty because the form is bound -- the value wasn't
     1274specified, and so is empty. This differs if the form were to be unbound:
     1275
     1276>>> p = UserRegistration()
     1277>>> print p['username'].value
     1278djangonaut
     1279
    12371280# Help text ###################################################################
    12381281
    12391282You can specify descriptive text for a field by using the 'help_text' argument
  • docs/ref/forms/fields.txt

     
    257257In the `built-in Field classes`_ section below, each ``Field`` defines the
    258258error message keys it uses.
    259259
     260Field data versus value
     261~~~~~~~~~~~~~~~~~~~~~~~
     262
     263.. versionadded:: 1.1
     264
     265.. attribute:: Field.value
     266.. attribute:: Field.data
     267
     268Sometimes, it's necessary -- or at least easier -- to render the form entirely
     269on your own. In such a case, you might want to get to what Django's form
     270rendering would use for the HTML `value` attribute.
     271
     272It'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
     280The value attribute takes initial data into account on form level (i.e.,
     281passing it to the initializer as seen in previous section), as well as field
     282level.
     283
     284The 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
    260291Built-in ``Field`` classes
    261292--------------------------
    262293
Back to Top