Python / Django Forms - Display Form Element Content Instead of Html Element
I am looping through some forms and I want to display some of the values of certain form elements. I have read that .data should take care of this for me. However, th开发者_如何学Pythonat returns "None" and the form does indeed have the field and correct stored value...
{% for document in documents: %}
{{ document.title.data }} <!-- NOTE: This returns "None" -->
{{ document.title }} <!-- Element with correct initial value -->
Any ideas?
Merci Beaucoup!!!
Update:
To access an initial value directly
forms.CharField(initial=X
)
Use{{ document.title.field.initial }}
.Accessing an initial value passed into the form constructor (
form = MyForm(initial={...})
) doesn't look possible via the template.BoundField
doesdata = self.form.initial.get(self.name, self.field.initial)
when rendering a widget to get at the forminitial
dict, then falls back to the form field's initial value.For a bound form where you've passed in data, such as
form = MyForm(request.POST)
, that data would be available in{{ document.title.data }}
In trunk, BoundField.value
returns either the initial or data! Cool stuff.
http://code.djangoproject.com/browser/django/trunk/django/forms/forms.py#L438
精彩评论