Submitting Form Values in Flask
To post and retrieve form values effectively in a Flask application, verify that your form elements have a unique and informative name attribute.
Problem:
In the provided code, the form fields lack the name attribute, causing request.form to remain empty and resulting in a 400 error when attempting to access values by ID.
<pre class="lang-html prettyprint-override"><input id="my_input" type="text" value="{{ email }}"> <input id="my_submit" type="submit" value="Submit"> </form>
Solution:
Attribute appropriate name values to your input elements:
<pre class="lang-html prettyprint-override"><input name="my_input" id="my_input" type="text" value="{{ email }}">
Once this is implemented, Flask will correctly interpret the submitted form data and make it accessible through request.form:
@app.route('/page', methods=['POST', 'GET']) def get_page(): if request.method == 'POST': print(request.form) # prints ImmutableMultiDict({ 'my_input': {{ email }}}) print(request.form['my_input']) # displays the value of 'my_input' return render_template('page.html')
The above is the detailed content of Why is my Flask Form Data not Being Submitted?. For more information, please follow other related articles on the PHP Chinese website!