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.
After adding
form.csrf_tokento the template (docs), the form will validate as expected.Add
print(form.errors)after validating the form to see the errors that were raised.errorswill be empty before validation. In this case, there is an error about missingI created an example on GitHub.