Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get form data in Flask? [duplicate]

You can get form data from Flask's request object with the form attribute:

from flask import Flask, request
app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    data = request.form['input_name']  # pass the form field name as key
    ...
    

You can also set a default value to avoid 400 errors with the get() method since the request.form attribute is a dict-like object:

from flask import Flask, request
app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    default_value = '0'
    data = request.form.get('input_name', default_value)
    ...