简体   繁体   English

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

[英]Passing dictionary to wtforms forms.py (*args, **kwargs) issues

Creating a flask app and having an issue passing a dictionary from my views.py page to my form.py page.创建一个 Flask 应用程序并在将字典从我的 views.py 页面传递到我的 form.py 页面时遇到问题。 The dictionary will be created dynamically based upon uploaded data.字典将根据上传的数据动态创建。 For now it is hardcoded.目前它是硬编码的。 I have to pass to create a dynamic number of fields.我必须通过创建动态数量的字段。

views.py视图.py

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

form.py表单.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))

The code works if I define select_dict in forms and only include the "for" loop.如果我在表单中定义 select_dict 并且只包含“for”循环,则代码有效。 Right now I keep getting this error: "formdata should be a multidict-type wrapper that supports the 'getlist' method"现在我不断收到这个错误:“formdata 应该是一个支持 'getlist' 方法的 multidict 类型包装器”

I switched the order of the arguments passed to the form.我切换了传递给表单的参数的顺序。 The correct way is below:正确方法如下:

form = F(select_dict, request.form)

Shot in the dark...what does your HTML look like to access the stuff from your form? 在黑暗中拍摄...您的HTML看起来是什么样子,可从表单中访问内容? I can see the data clear as day when printing from the form in the console, but if I try to dump all fields/data in the HTML none of SelectMultipleField stuff exists. 从控制台中的表单打印时,我可以看到当天的数据清除,但是如果我尝试转储HTML中的所有字段/数据,则不会存在SelectMultipleField内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM