gpt4 book ai didi

python - 在 Flask 应用程序中提交表单时出现错误请求错误的原因是什么?

转载 作者:IT老高 更新时间:2023-10-28 21:33:32 24 4
gpt4 key购买 nike

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

400 Bad Request

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

虽然表单始终正确显示,但当我提交与以下任一功能相关的 HTML 表单时会出现错误请求:

@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的相关部分是

<html>
<head>
<title>apply</title>
</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="your@email.com">
...

大多数人的问题是没有调用 GETPOST,但我在这两个函数中都这样做了,我仔细检查以确保我导入了所有必要的内容,如from flask import request。我还查询了数据库,确认没有添加表单中的添加内容。

在 Flask 应用程序中,我请求的表单字段在 HTML 表单中的标记略有不同。保持名称一致是必须的。更多内容可以阅读这个问题 Form sending error, Flask

最佳答案

解决方案很简单,并且在评论中没有发现。如本问题所述,Form sending error, 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 中,我标记了各种表单输入

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

在 Python 中,当调用 POST 时,我用

抓取了一个不存在的表单
request.form['question1']

然而,为了与我的 HTML 表单名称保持一致,它必须是

request.form['question1_field']

我希望这会有所帮助。

关于python - 在 Flask 应用程序中提交表单时出现错误请求错误的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14105452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com