WTForm i数组 - UnboundField FormField

时间:2014-12-04 10:33:46

标签: python jinja2 wtforms flask-wtforms

我是使用WTForms和Python的初学者。 我在字典或列表中聚合WTForm时遇到问题,通过jinja2渲染。 例如:

class CJanuary(Form):
    nr          = int(12)
    netto       = DecimalField(u'Salary netto',         default = 0, places = 2)
    brutto      = DecimalField(u'Salary brutto',        default = 0, places = 2)    

class InputMonthlyForm(Form):
    january = FormField(CJanuary)
    months  = [FormField(CJanuary)]

如果我将其渲染如下:

{{form.january.netto}}

在这种情况下一切正常,我得到了来源:

<input id="january-netto" name="january-netto" type="text" value="0.00">

但如果我从列表中尝试渲染:

{{form.months[0].netto}}

在源头我什么都没得到。

检查列表中的内容:

{{form.months[0]}}

我明白了:

<UnboundField(FormField, (<class 'apps.placowy.forms.InputMonthlyForm.CJanuary'>,), {})>

我搜索了互联网,但找不到任何解决方案。 是否可能无法在阵列中聚合WTForm?

1 个答案:

答案 0 :(得分:0)

尝试使用FieldList代替list:

class CJanuary(Form):
    nr          = int(12)
    netto       = DecimalField(u'Salary netto',         default = 0, places = 2)
    brutto      = DecimalField(u'Salary brutto',        default = 0, places = 2)    

class InputMonthlyForm(Form):
    january = FormField(CJanuary)
    months  = FieldList(FormField(CJanuary))