将字典传递给wtforms forms.py(* args,** kwargs)问题

时间:2016-05-02 17:53:24

标签: python flask wtforms

创建一个烧瓶应用程序并将问题从我的views.py页面传递到我的form.py页面时遇到问题。将根据上载的数据动态创建字典。现在它是硬编码的。我必须通过创建动态数量的字段。

views.py

select_dict = {'Geography': ['US', 'Asia', 'Europe'], 'Product Type': ['X', 'Y', 'Z']}
form = F(request.form,select_dict)

form.py

class SimpleForm(Form):
    list_of_files = ['Option 1','Option 2','Option 3','Option 4','Option 5','Option 6']
    files = [(x, x) for x in list_of_files]
    acheckbox = MultiCheckboxField('Label',choices=files)
    third_list = ['Special Analysis']
    third_files = [(x, x) for x in third_list] 
    bcheckbox = MultiCheckboxField('Label', choices=third_files)
    category_1 = SelectField(u'', choices=())
    category_2 = SelectField(u'', choices=())
    category_3 = SelectField(u'', choices=())

class F(SimpleForm):
    pass

    def __init__(self, select_dict, *args, **kwargs):
        super(SimpleForm, self).__init__(*args, **kwargs)
        print(select_dict)
        for name,choices in select_dict.items():
            test = [(x, x) for x in choices]
            setattr(F, name, SelectMultipleField(name.title(),choices=test))

如果我在表单中定义select_dict并且只包含" for"环。现在我一直收到这个错误:" formdata应该是一个multidict类型的包装器,它支持' getlist'方法"

1 个答案:

答案 0 :(得分:0)

我切换了传递给表单的参数的顺序。正确的方法如下:

form = F(select_dict, request.form)