How to send data from a textbox into Python Flask?

Sometimes, we want to send data from a textbox into Python Flask.

In this article, we’ll look at how to send data from a textbox into Python Flask.

How to send data from a textbox into Python Flask?

To send data from a textbox into Python Flask, we can get the form values from request.form in our view.

For instance, we write

<form method="POST">
    <input name="text">
    <input type="submit">
</form>

to add a form into the templates/my-form.html file.

Then in our app’s code, we write

from flask import Flask, request, render_template

app = Flask(__name__)

@app.route('/')
def my_form():
    return render_template('my-form.html')

@app.route('/', methods=['POST'])
def my_form_post():
    text = request.form['text']
    processed_text = text.upper()
    return processed_text

to render templates/my-form.html within the my_form route.

And we get the form values from request.form in the my_form_post route.

We get the input with the name attribute text by using request.form['text'].

Conclusion

To send data from a textbox into Python Flask, we can get the form values from request.form in our view.