Home  >  Article  >  Backend Development  >  Why is my Flask Form Data not Being Submitted?

Why is my Flask Form Data not Being Submitted?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-30 09:38:27760browse

Why is my Flask Form Data not Being Submitted?

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn