Categories
Python Answers

How to send data from HTML form to a Python script in Flask?

Spread the love

Sometimes, we want to send data from HTML form to a Python script in Flask.

In this article, we’ll look at how to send data from HTML form to a Python script in Flask.

How to send data from HTML form to a Python script in Flask?

To send data from HTML form to a Python script in Flask, we can use the request.form dictionary to get the data.

For instance, w ewrite

from flask import request

@app.route('/add_region', methods=['POST'])
def add_region():
    #...
    return (request.form['file_path'])

to get the form data with the request.form dict.

And then we create our form with

<form action="{{ url_for('add_region') }}" method="post">
    Project file path: <input type="text" name="file_path"><br>
    <input type="submit" value="Submit">
</form>

in our template.

request.form['file_path'] has the value of the input with name attribute set to file_path in our form.

action is {{ url_for('add_region') }} which is the URL to the add_region endpoint.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *