diff --git a/django/forms/forms.py b/django/forms/forms.py index 0377de4767..1cf6f2a3a2 100644 --- a/django/forms/forms.py +++ b/django/forms/forms.py @@ -432,20 +432,11 @@ class BoundField(StrAndUnicode): else: attrs['id'] = self.html_initial_id - if not self.form.is_bound: - data = self.form.initial.get(self.name, self.field.initial) - if callable(data): - data = data() - else: - data = self.field.bound_data( - self.data, self.form.initial.get(self.name, self.field.initial)) - data = self.field.prepare_value(data) - if not only_initial: name = self.html_name else: name = self.html_initial_name - return widget.render(name, data, attrs=attrs) + return widget.render(name, self.value(), attrs=attrs) def as_text(self, attrs=None, **kwargs): """ @@ -470,6 +461,21 @@ class BoundField(StrAndUnicode): return self.field.widget.value_from_datadict(self.form.data, self.form.files, self.html_name) data = property(_data) + def value(self): + """ + Returns the value for this BoundField, using the initial value if + the form is not bound or the data otherwise. + """ + if not self.form.is_bound: + data = self.form.initial.get(self.name, self.field.initial) + if callable(data): + data = data() + else: + data = self.field.bound_data( + self.data, self.form.initial.get(self.name, self.field.initial) + ) + return self.field.prepare_value(data) + def label_tag(self, contents=None, attrs=None): """ Wraps the given contents in a