Skip to content

Receiving Data in Flask

The form is ready. Now you need Flask to catch what the user submits, store it, and send the updated page back.


Two new imports

You need two more things from Flask:

from flask import Flask, render_template, request, redirect, url_for
  • request — gives you access to what the user sent (the form data)
  • redirect — sends the user to a different route after the form is submitted
  • url_for — generates the URL for a route by its function name

The storage

At the top of app.py, outside any route, create an empty list. This is where every submitted person will be stored while the app is running:

people = []

This lives in memory — it is just a Python list. It starts empty every time you run the app, and fills up as users submit the form. That is fine for now.


The /add route

@app.route('/add', methods=['POST'])
def add():
    person = {
        "name":   request.form["name"],
        "age":    int(request.form["age"]),
        "gender": int(request.form["gender"])
    }
    people.append(person)
    return redirect(url_for('home'))

Going through it:

methods=['POST'] — this route only accepts POST requests. The form sends POST. A regular browser visit is GET — if someone tries to visit /add directly in the browser, Flask will reject it.

request.form["name"] — reads the value of the field whose name attribute is "name" from the submitted form. This is exactly why those name attributes in your HTML matter.

int(request.form["age"]) — form data always arrives as text. "24" is not the same as 24. You convert it to an integer so it stores correctly.

people.append(person) — adds the new dictionary to the list.

return redirect(url_for('home')) — instead of rendering a template directly, you send the user back to the / route. The home route then renders the page with the updated people list. This prevents the browser from asking "do you want to resubmit the form?" if the user refreshes.


The home route — updated

The home route now passes the people list to the template:

@app.route('/')
def home():
    return render_template('index.html', people=people)

When the user is redirected here after submitting, people already has the new entry in it.


The complete app.py

from flask import Flask, render_template, request, redirect, url_for

app = Flask(__name__)

people = []

@app.route('/')
def home():
    return render_template('index.html', people=people)

@app.route('/add', methods=['POST'])
def add():
    person = {
        "name":   request.form["name"],
        "age":    int(request.form["age"]),
        "gender": int(request.form["gender"])
    }
    people.append(person)
    return redirect(url_for('home'))

if __name__ == '__main__':
    app.run(debug=True)

Challenge

Update app.py with the code above. Run the app. Fill in the form and click Add Person. Does the page reload and show the entry in the table? Submit a few more — does the table grow with each one?


← Building the Form    Next: Showing the Updated List →