What is the reason for the erroneous request when submitting the form to the Flask application?

After reading many similar sounding issues and related Flask docs, I can't figure out what causes the following error when submitting the form:

400 Bad Request

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

As long as the form always displays correctly, an unsuccessful request occurs when I submit the HTML form associated with any 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") 

Relevant HTML Part

 <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 did not cause GET or POST , but I only do this in both functions, and I double-checked to make sure that I imported everything I needed, for example from flask import request . I also requested a database and confirmed that no additions from the form were added.

In a Flask application, I requested form fields that were slightly different in the HTML form. Keeping consistent names is mandatory. You can read more on this subject. Error sending form, check box.

+74
python post flask forms bad-request
Dec 31 '13 at 19:36
source share
1 answer

The solution was simple and open in the comments. In this question, Error sending form, checkbox and indicated by Sean Vieira ,

... the problem is that Flask causes an HTTP error when it cannot find the key in the args and form dictionaries. What Flux assumes by default is that if you request a specific key, and this is not the case, something has left 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 invalid and an error appears, in my case, without any violations in the trace. For me it was a lack of consistency with the writing: in HTML, I designated the various inputs of the form

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

and in Python, when POST was called, I get a non-existent form with

 request.form['question1'] 

whereas in order to match my names in HTML form, it must be

 request.form['question1_field'] 

Hope this helps.

+129
Jan 01 '13 at 20:28
source share



All Articles