Difference between `form.validate_on_submit()` and `form.validate()`

大憨熊 提交于 2019-12-18 15:49:57

问题


What is the difference between form.validate_on_submit() and form.validate()?

In the Flask WTF docs, the author uses form.validate_on_submit(). The code works. When I substitute form.validate_on_submit() with form.validate(), I see no difference in behavior. I couldn't find form.validate_on_submit() in the source, but I managed to find form.validate() code.


回答1:


validate_on_submit() is a shortcut for is_submitted() and validate().

From the source code, line 89, is_submitted() returns True if the form submitted is an active request and the method is POST, PUT, PATCH, or DELETE.

Generally speaking, it is used when a route can accept both GET and POST methods and you want to validate only on a POST request.



来源:https://stackoverflow.com/questions/43002323/difference-between-form-validate-on-submit-and-form-validate

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