开发者

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:

  1. To access an initial value directly forms.CharField(initial=X)
    Use {{ document.title.field.initial }}.

  2. Accessing an initial value passed into the form constructor (form = MyForm(initial={...})) doesn't look possible via the template. BoundField does data = self.form.initial.get(self.name, self.field.initial) when rendering a widget to get at the form initial dict, then falls back to the form field's initial value.

  3. 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜