Creating a flask app and having an issue passing a dictionary from my views.py page to my form.py page. 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
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))
The code works if I define select_dict in forms and only include the "for" loop. Right now I keep getting this error: "formdata should be a multidict-type wrapper that supports the 'getlist' method"
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? 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.