Flask 视图显示 400 错误而不是带有表单的模板 [英] Flask view shows 400 error instead of template with form

查看:17
本文介绍了Flask 视图显示 400 错误而不是带有表单的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图显示一个带有表单的页面,然后在提交表单时向数据库添加一个 Player.但是,我无法查看表单,因为浏览器总是显示 400 Bad Request 错误.其他帖子表明这可能是因为表单输入的名称与我从 request.form 获得的键不匹配,但我所有的键都匹配.为什么会出现此错误?

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)

推荐答案

您的视图接受 GETPOST 请求.request.form 仅在 POST 上填写.如果您尝试访问不存在的密钥,则会引发 400 错误.当您最初GET页面时,将不存在任何键.

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.

对此的常见模式是在 if request.method == 'POST' 块中保护需要 request.form 的代码.处理完 POST 请求后返回重定向,否则返回渲染的模板.

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')

<小时>

由于您似乎在使用 Flask-WTF,您可以使用表单的 validate_on_submit 方法而不是检查 method.在这种情况下,您还可以通过表单实例访问数据,并使用表单为您呈现输入.


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>

这篇关于Flask 视图显示 400 错误而不是带有表单的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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