在Flask应用程序中提交表单时,请求错误的原因是什么? [英] What is the cause of the Bad Request Error when submitting form in Flask application?
问题描述
lockquote
<400>错误请求
浏览器(或代理)发送了一个请求,表示这个服务器不能理解。
虽然表单总是正确显示,但是当我提交一个绑定到这两个函数的HTML表单时,会发生错误的请求:
<$如果有请求,
$ apply $ business():
$ app $($ / $ / $)方法=='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 = req (),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']
返回重定向(url_for('success'))
返回render_template('application.html',accounttype =business)
@ app.route('/ app / student',methods = ['GET','POST'])$ b $如果request.method =='POST':
new_account = Student(name = request.form ['name_field'],email = request.form ['email_field'],$ b $ apply_student 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_fi ('),
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>全名:< / p>
< input name =name_fieldplaceholder =First and Last>
< p>电子邮件地址:< / p>
< input name =email_fieldplaceholder =your@email.com>
...
大多数人的问题不是调用 GET
或 POST
,但是我在两个函数中都做了这个,而且我再次检查以确保我导入了所有必需的东西,比如从瓶子导入请求
。我也查询了数据库,并确认添加的表单没有被添加。
在Flask应用程序中,我要求表单字段在标签上略有不同HTML表单。保持名称一致是必须的。更多可以阅读这个问题表单发送错误,Flask
解决方案很简单,并且在评论中发现。正如在这个问题中解决,表单发送错误,Flask ,并指出了由 Sean Vieira ,
$ b
...问题是Flask在args和表单字典中找不到
键时会引发HTTP错误。 Flask假定默认的
是,如果你要求一个特定的键而不在那里,那么
的东西就不在请求中,而整个请求是
无效。
换句话说,如果只有一个您在Python中请求的表单元素不能在HTML中找到,那么POST请求无效并且出现错误,在我的情况下没有任何违规追踪。对我来说,这与拼写不一致:在HTML中,我标记了各种表单输入
< input name = question1_fieldplaceholder =问题一>
在Python中,当有POST被调用时,我用
$ b $
request.form ['question1']
然而,为了与我的HTML表单名称一致,它需要是
$ $ p $ request.form [ 'question1_field']
我希望这可以帮到您。
After reading many similar sounding problems and the relevant Flask docs, I cannot seem to figure out what is generating the following error upon submitting a form:
400 Bad Request
The browser (or proxy) sent a request that this server could not understand.
While the form always displays properly, the bad request happens when I submit an HTML form that ties to either of these functions:
@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")
The relevant part of HTML is
<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">
...
The problem for most people was not calling GET
or POST
, but I am doing just that in both functions, and I double checked to make sure I imported everything necessary, such as from flask import request
. I also queried the database and confirmed that the additions from the form weren't added.
In the Flask app, I was requesting form fields that were labeled slightly different in the HTML form. Keeping the names consistent is a must. More can be read at this question Form sending error, Flask
The solution was simple and uncovered in the comments. As addressed in this question, Form sending error, Flask, and pointed out by 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.
In other words, if only one form element that you request in Python cannot be found in HTML, then the POST request is not valid and the error appears, in my case without any irregularities in the traceback. For me, it was a lack of consistency with spelling: in the HTML, I labeled various form inputs
<input name="question1_field" placeholder="question one">
while in Python, when there was a POST called, I grab a nonexistent form with
request.form['question1']
whereas, to be consistent with my HTML form names, it needed to be
request.form['question1_field']
I hope this helps.
这篇关于在Flask应用程序中提交表单时,请求错误的原因是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!