Flask-WTF - validate_on_submit()永远不会执行 [英] Flask-WTF - validate_on_submit() is never executed

查看:166
本文介绍了Flask-WTF - validate_on_submit()永远不会执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Flask-WTF:

这是我的表单:

  from flask.ext.wtf import Form,TextField 
$ b $ class BookNewForm(Form):
name = TextField('Name')



$ p $ book.route('/ book / new',methods = ['GET','POST'])
def customers_new():
form = BookNewForm()
if form.is_submitted ):
printsubmitted
if form.validate():
printvalid
if form.validate_on_submit():
flash(成功创建新书)
return redirect(url_for('books_show'))
return render_template('views / books_new.html',form = form)

现在的问题是,如果你看我的打印语句,它总是打印提交,但它永远不会打印有效和validate_on_submit()永远不会执行。为什么?

解决方案

你没有在HTML表单中插入CSRF字段。

 < form method = post> 
{{form.csrf_token}}
{{form.name}}
< input type = submit>
< / form>

在模板中添加 form.csrf_token 文档),表单将按预期进行验证。

在验证窗体后,添加 print(form.errors)以查看引发的错误。验证前,错误将为空。在这种情况下,有一个关于错过的错误

  @ book.route('/ book / new_no_csrf',methods = [' ())
def customers_new_no_csrf():$ b $ form = BookNewForm()
print(form.errors)

if form.is_submitted() :
printsubmit

if form.validate():
printvalid

print(form.errors)

if form.validate_on_submit():
flash(成功创建新书)
返回重定向(url_for('。books_show'))

return render_template ('books_new.html',form = form)





  {} 
已提交
{'csrf_token':[u'CSRF token missing']}
127.0.0.1 - - [29 / May / 2012 02:01 :08]POST / book / new_no_csrf HTTP / 1.1200 -
127.0.0.1 - - [29 / May / 2012 02:01:08]GET /favicon.ico HTTP / 1.1404 -

我在GitHub上创建了一个例子


I'm using Flask-WTF:

Here is my form:

from flask.ext.wtf import Form, TextField

class BookNewForm(Form):
    name = TextField('Name')

Here is the controller:

@book.route('/book/new', methods=['GET', 'POST'])
def customers_new():
    form = BookNewForm()
    if form.is_submitted():
        print "submitted"
    if form.validate():
        print "valid"
    if form.validate_on_submit():
        flash("Successfully created a new book")
        return redirect(url_for('.books_show'))
    return render_template('views/books_new.html', form=form)

Now the problem is, if you look at my print statements, it always prints submitted, but it NEVER prints valid and validate_on_submit() is never executed. Why?

解决方案

You're not inserting the CSRF field in the HTML form.

<form method=post>
    {{ form.csrf_token }}
    {{ form.name }}
    <input type=submit>
</form>

After adding form.csrf_token to the template (docs), the form will validate as expected.

Add print(form.errors) after validating the form to see the errors that were raised. errors will be empty before validation. In this case, there is an error about missing

@book.route('/book/new_no_csrf', methods=['GET', 'POST'])
def customers_new_no_csrf():
    form = BookNewForm()
    print(form.errors)

    if form.is_submitted():
        print "submitted"

    if form.validate():
        print "valid"

    print(form.errors)

    if form.validate_on_submit():
        flash("Successfully created a new book")
        return redirect(url_for('.books_show'))

    return render_template('books_new.html', form=form)

{}
submitted
{'csrf_token': [u'CSRF token missing']}
127.0.0.1 - - [29/May/2012 02:01:08] "POST /book/new_no_csrf HTTP/1.1" 200 -
127.0.0.1 - - [29/May/2012 02:01:08] "GET /favicon.ico HTTP/1.1" 404 -

I created an example on GitHub.

这篇关于Flask-WTF - validate_on_submit()永远不会执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆