2014-12-04 19 views
0

我是使用WTForms和Python的初學者。 我有一個字典或列表中的WTForm的聚合問題,通過jinja2呈現。 例如:WTForm我陣列 - UnboundField FormField

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嗎?

+0

但爲什麼要在上帝的名義做你需要把formfield在列表中? – 2014-12-04 10:44:06

+0

能夠在模板中的mounths上迭代嗎?爲什麼你將這件事與上帝聯繫起來 – user3471671 2014-12-04 15:22:39

回答

0

嘗試使用FieldList,而不是名單:

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)) 
+0

它的作品!謝謝 – user3471671 2014-12-04 14:29:49

+0

不,它只適用於兩個項目,如果我嘗試添加三個項目得到:文件「/usr/local/lib/python2.7/dist-packages/wtforms/fields/core.py」,行109,在__init__ 顯示此框架的源代碼在self.validators中的此框架中打開一個交互式python shell: TypeError:'UnboundField'對象不可迭代 – user3471671 2014-12-12 12:38:39