Flask WTForms form does not validate

你。 提交于 2019-12-25 03:49:33

问题


I cannot get a certain simple Flask WTForm to validate.

After a couple of days of struggle, I've tried everything I can think of. I'm new to Flask and Web programming in general.

Here's a stripped-down but working version of my code. The only action of the test code (other than submitting the form) is to print messages to terminal. Its appearance when running:

This is views.py:

    # -*- coding: utf_8 -*-
...

@app.route('/test/new/', methods=['GET','POST'])
def newTest():
    form = TestForm(request.form)
    if form:
        print 'request.form.get(\'name\') is %s' % (request.form.get('name'),)
    if request.method == 'POST':
        print 'in newTest(), request is POST'
        if form.validate():
            print 'form validates'
            return redirect(url_for('allTests'))
        else:
            print 'form does not validate'
            return render_template('newTest.html', form=form)
    else:
        return render_template('newTest.html', form=form)

Here's forms.py:

class TestForm(Form):
    name = StringField(u"Test Name", [validators.InputRequired()])
    address = StringField(u"Test Address", [validators.InputRequired()])

    submit = SubmitField(u"Submit")

models.py:

from sqlalchemy import Column, Integer, Unicode
from sqlalchemy.ext.declarative import declarative_base

Base = declarative_base()

class TestModel(Base):

    __tablename__ = 'test'

    name = Column(Unicode(80), nullable = False)
    id = Column(Integer, primary_key = True)
    address = Column(Unicode(80), nullable = False)

and the template:

<html lang="en">
<head>
    <meta charset="utf-8">
    <title>New Test</title>
</head>
<body>
    <div>
        <form action="{{ url_for('newTest') }}" method="POST" name="add_rest">
        <ul>
            <li>Name: {{ form.name }}</li>
            <li>Address: {{ form.address }}</li>
        </ul>
        <input type="submit" value="Create"> 
    </div>
</body>
</html>

The output (to terminal) I get when I click "Create" above:

request.form.get('name') is Vinnie
in newTest(), request is POST
form does not validate
10.0.2.2 - - [18/Feb/2016 02:34:51] "POST /test/new/ HTTP/1.1" 200 -

then the browser redisplays the form and its contents.

I assume I'm missing something simple, but I haven't been able to figure this out for the life of me.

The structure of the code, as shown by "tree", is:

I'd be very grateful for any help!


回答1:


Have you tried to insert a CSRF token in your HTML file?

For example, by adding the following to your Jinja template?

<body>
    <div>
        <form action="{{ url_for('newTest') }}" method="POST" name="add_rest">
        <!-- Added line -->
        {{ form.csrf_token }}
        <ul>
            <li>Name: {{ form.name }}</li>
            <li>Address: {{ form.address }}</li>
        </ul>
        <input type="submit" value="Create"> 
    </div>
</body>

This SO post may be useful.

You can also check the official documentation here which indicates that the validate() function requires the use of a CSRF token.



来源:https://stackoverflow.com/questions/35472322/flask-wtforms-form-does-not-validate

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