python flask get user input

Solutions on MaxInterview for python flask get user input by the best coders in the world

showing results for - "python flask get user input"
Chaima
11 Jul 2018
1Unless you want to do something more complicated, feeding data from a HTML form into Flask is pretty easy.
2
3Create a view that accepts a POST request (my_form_post).
4Access the form elements in the dictionary request.form.
5templates/my-form.html:
6
7<form method="POST">
8    <input name="text">
9    <input type="submit">
10</form>
11from flask import Flask, request, render_template
12
13app = Flask(__name__)
14
15@app.route('/')
16def my_form():
17    return render_template('my-form.html')
18
19@app.route('/', methods=['POST'])
20def my_form_post():
21    text = request.form['text']
22    processed_text = text.upper()
23    return processed_text
24This is the Flask documentation about accessing request data.
25
26If you need more complicated forms that need validation then you can take a look at WTForms and how to integrate them with Flask.
27
28Note: unless you have any other restrictions, you don't really need JavaScript at all to send your data (although you can use it).