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.