Flask view shows 400 error instead of template with form

浪子不回头ぞ 提交于 2019-11-26 08:37:47

问题


I\'m trying to display a page with a form, then add a Player to the database when the form is submitted. However, I can\'t view the form because the browser always shows a 400 Bad Request error. Other posts indicate that this could be because the name of the form input doesn\'t match the key I get from request.form, but all my keys match. Why do I get this error?

<form method=\"post\">
    {{ form.hidden_tag() }}
    <input name=\"name\">
    <input name=\"available\">
    <input type=\"submit\">
</form>
@app.route(\'/addplayer\', methods=[\'GET\', \'POST\'])
def addplayer():
    connect(\'basketball_contracts\', host=\'localhost\', port=27017)
    n = request.form[\'name\']
    a = request.form[\'available\']
    post= Post(
        name=n,
        available=a
    )
    post.tags = [\'test\']
    post.save()
    return render_template(\'addplayer.html\', form=form)

回答1:


Your view accepts GET and POST requests. request.form is only filled out on POST. If you try to access a key that doesn't exist, it raises a 400 error. No keys will exist when you GET the page initially.

The common pattern for this is to guard code that requires request.form in an if request.method == 'POST' block. Return a redirect after handling the POST request, otherwise return the rendered template.

from flask import url_for, redirect, render_template

@app.route('/addplayer', methods=['GET', 'POST'])
def addplayer():
    if request.method == 'POST':
        Post(
            name=request.form['name'],
            available=request.form['available']
        ).save()
        return redirect(url_for('index'))

    return render_template('addplayer.html')

Since you appear to be using Flask-WTF, you can use the form's validate_on_submit method instead of checking method. In that case, you can also access the data through the form instance, and use the form to render the inputs for you.

from flask import url_for, redirect, render_template

@app.route('/addplayer', methods=['GET', 'POST'])
def addplayer():
    form = AddPlayerForm()

    if form.validate_on_submit():
        Post(
            name=form.name.data,
            available=form.available.data
        ).save()
        return redirect(url_for('index'))

    return render_template('addplayer.html', form=form)
<form method=post>
    {{ form.hidden_tag() }}
    {{ form.name.label}} {{ form.name }}<br>
    {{ form.available.label }} {{ form.available }}<br>
    <input type=submit value="Add Player">
</form>


来源:https://stackoverflow.com/questions/46280558/flask-view-shows-400-error-instead-of-template-with-form

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