【问题标题】:Bad request error flask with POST request带有 POST 请求的错误请求错误烧瓶
【发布时间】:2016-05-04 01:31:20
【问题描述】:

我有以下从提交的表单中获取值的路线

@app.route('/authenticate', methods=["POST"])
def authenticate():
    username = request.form['username']
    print(username, file = sys.stderr)

    password = request.form['password']
    email = request.form['email']

    models.User.create_user(email, password, username)

    return render_template('signup.html')

我遇到的问题是我收到了Bad Request The browser (or proxy) sent a request that this server could not understand.我已经检查过我是否正确地从表单中获取了值,并且所有表单中都有内容,但它似乎不起作用.

这里是渲染视图的模板

<form action ="/authenticate" method="POST" id="signup">
                <fieldset class="form-group">
                    <label for="InputUsername"> Username </label>
                    <input type="text" class="form-control" name="username" id="InputUsername" placeholder="Enter username">
                </fieldset>
                <fieldset class="form-group">
                    <label for="exampleInputEmail1">Email address</label>
                    <input type="email" class="form-control" name="email"id="exampleInputEmail1" placeholder="Enter email">
                    <small class="text-muted">We'll never share your email with anyone else.</small>
                  </fieldset>
                  <fieldset class="form-group">
                    <label for="exampleInputPassword1">Password</label>
                    <input type="password" class="form-control" name="password "id="exampleInputPassword1" placeholder="Password">
                </fieldset>
            </form>

这是我的表单所在的视图类

@app.route('/signup')
def login():
    return render_template("signup.html")

【问题讨论】:

    标签: python post flask


    【解决方案1】:

    99% 的情况下,此错误是由于您请求 request.form 字典中不存在的键而导致的键错误。要调试它,请运行

    print(request.form)
    

    并确保您请求的每个键都存在于字典中。这个错误特别令人沮丧(而且看起来很神秘),因为它不会触发您通常在 Flask 中获得的通常的回溯。

    看看这些问题:

    What is the cause of the Bad Request Error .. ?
    Form sending error, Flask

    【讨论】:

    • 这是我打印出来的,但我只调用这些值。 ImmutableMultiDict([('username', u'ralphie9224'), ('password', u'MAVS.man1'), ('email', u'rmoreno.cesar@gmail.com')])
    • 啊。对所有输入使用相同的名称,但添加不同的值。像这样: 如果它有效,我会编辑我的答案。
    猜你喜欢
    • 2018-05-10
    • 2017-06-02
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 1970-01-01
    • 2018-08-29
    • 2022-06-28
    • 2015-03-10
    相关资源
    最近更新 更多