Post values ​​from an HTML form and access them as a flag

I have an HTML form that submits to a Flask route. However, request.form empty. If I try to access one of the values ​​by id, I get an error of 400. How do I submit values ​​from an HTML form and access them in Flask?

 <form method="POST"> <input id="my_input" type="text" value="{{ email }}"> <input id="my_submit" type="submit" value="Submit"> </form> 
 @app.route('/page', methods=['POST', 'GET']) def get_page(): if request.method == 'POST': print(request.form) # prints ImmutableMultiDict([]) print(request.form['my_input']) # raises 400 error return render_template('page.html') 
+5
source share
1 answer

There is no name attribute in your input . This is what the client will transmit to the server. The checkbox will cause a 400 error when accessing a form key that has not been submitted.

 <input name="my_input" id="my_input" type="text" value="{{ email }}"> 
+5
source

Source: https://habr.com/ru/post/1201850/


All Articles