I'm having problems with wtforms selectfields when i use a POST with Flask

て烟熏妆下的殇ゞ 提交于 2019-11-30 11:17:14

You need to set your choices before you call validate_on_submit as form.validate will attempt to validate the provided value (if any) against the list of choices (which is None before you set choices):

form = PostForm()
form.hometest.choices = [(h.key.id(), h.homename) for h in Home.query()]

if form.validate_on_submit():
    # form is valid, continue

You should provide choices=[...] argument, like

wtf.SelectField(u'Home Name List',
                choices=[(1, 'Label 1'),
                         (2, 'Label 2')],
                coerce=int,
                validators=[validators.optional()])
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!