Setting default value after initialization in SelectField flask-WTForms

荒凉一梦 提交于 2019-12-05 14:09:37

Once an instance of the form is created, the data is bound. Changing the default after that doesn't do anything. The reason changing choices works is because it affects validation, which doesn't run until validate is called.

Pass default data to the form constructor, and it will be used if no form data was passed. The default will be rendered the first time, then posted the second time if the user doesn't change the value.

form = AddressForm(request.form, country='US')

(If you're using Flask-WTF's Form you can leave out the request.form part.)

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