关于python:在Flask应用程序中提交表单时,错误请求错误的原因是什么?

What is the cause of the Bad Request Error when submitting form in Flask application?

在阅读了许多类似的发音问题和相关的Flask文档之后,我似乎无法弄清楚提交表单后是什么导致了以下错误:

400 Bad Request

The browser (or proxy) sent a request that this server could not understand.

虽然表单始终可以正确显示,但是当我提交与以下两个功能之一关联的HTML表单时,就会发生错误的请求:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@app.route('/app/business', methods=['GET', 'POST'])
def apply_business():
    if request.method == 'POST':    
        new_account = Business(name=request.form['name_field'], email=request.form['email_field'], account_type="business",
            q1=request.form['q1_field'], q2=request.form['q2_field'], q3=request.form['q3_field'], q4=request.form['q4_field'],
            q5=request.form['q5_field'], q6=request.form['q6_field'], q7=request.form['q7_field'],
            account_status="pending", time=datetime.datetime.utcnow())
        db.session.add(new_account)
        db.session.commit()
        session['name'] = request.form['name_field']    
        return redirect(url_for('success'))
    return render_template('application.html', accounttype="business")          

@app.route('/app/student', methods=['GET', 'POST'])
def apply_student():    
    if request.method == 'POST':    
        new_account = Student(name=request.form['name_field'], email=request.form['email_field'], account_type="student",
            q1=request.form['q1_field'], q2=request.form['q2_field'], q3=request.form['q3_field'], q4=request.form['q4_field'],
            q5=request.form['q5_field'], q6=request.form['q6_field'], q7=request.form['q7_field'], q8=request.form['q8_field'],
            q9=request.form['q9_field'], q10=request.form['q10_field'],
            account_status="pending", time=datetime.datetime.utcnow())
        db.session.add(new_account)
        db.session.commit()
        session['name'] = request.form['name_field']    
        return redirect(url_for('success'))
    return render_template('application.html', accounttype="student")

HTML的相关部分是

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<html>
<head>
    apply
</head>
<body>
    {% if accounttype=="business" %}
    <form action="{{ url_for('apply_business') }}" method=post class="application_form">
    {% elif accounttype=="student" %}
    <form action="{{ url_for('apply_student') }}" method=post class="application_form">    
    {% endif %}
    <p>
Full Name:
</p>
    <input name="name_field" placeholder="First and Last">
    <p>
Email Address:
</p>
    <input name="email_field" placeholder="[email protected]">
    ...

对于大多数人来说,问题不是调用GETPOST,而是我在两个函数中都这样做,并且再次检查以确保导入了所有必需的内容,例如from flask import request。 我还查询了数据库,并确认未添加表单中的其他内容。

在Flask应用程序中,我请求的表单字段在HTML表单中被标记为稍有不同。 必须保持名称一致。 有关此问题的更多信息,请阅读表格发送错误,烧瓶


该解决方案很简单,并且在评论中没有发现。 正如这个问题所提及的,表单发送错误Flask,并由Sean Vieira指出,

...the issue is that Flask raises an HTTP error when it fails to find a
key in the args and form dictionaries. What Flask assumes by default
is that if you are asking for a particular key and it's not there then
something got left out of the request and the entire request is
invalid.

换句话说,如果在HTML中仅找不到您在Python中请求的一个表单元素,则POST请求无效并且出现错误,在我的情况下,回溯中没有任何异常。 对我来说,拼写缺乏一致性:在HTML中,我标记了各种表单输入

1
<input name="question1_field" placeholder="question one">

在Python中,当有一个POST调用时,我抓取了一个不存在的表单

1
request.form['question1']

但是,为了与我的HTML表单名称保持一致,

1
request.form['question1_field']

我希望这有帮助。