flask-wtf selectField choices not valid

烈酒焚心 提交于 2019-12-06 04:47:22

Maybe the problem is that your keys in dict are a strings. I had this issue before, so maybe something like this would help:

typeone = SelectField("Question1", coerce=str, choices=QUESTION_LIST['QuestionOne'])

This coerce thingie helped. What happens I think that all POST data is unicode and by default coerce is also equals to unicode (at least in WTF forms, need to check Flask-WTF extension if you do use one). And your choices keys are strings.

You are passing a set instead of a dict. Replace the commas in your QUESTION_LIST definition with colons. Actually the call to .iteritems() should already fail...

QUESTION_LIST['QuestionOne'] = { 'disagree-strong': "Strongly Disagree",
                                 'agree-strong': "Strongly Agree" }

For some reason, when you put the iteritems in the view instead of the form ,everything works. Some sort of quirk with Flask-WTF where if you don't use their format it seems to delete the choices after you submit form.

So just moving the .iteritems() code into the VIEW, by typing form.question_field.choices = QUESTION_LIST['QuestionOne'].iteritems(); Works better than using iteriterms inside the form file.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!