When an HTML form is submitted to Flask, every value lands in request.form — a special dict that can hold several values for the same key. Use request.form.get() for single values, request.form.getlist() for checkboxes or multi-selects, and request.form.get("age", type=int) to convert types safely. Remember these three calls and you’ll read every form correctly.
The Problem
Take a signup form with three fields: name (text), age (number) and topics of interest (checkboxes, pick many). Code that reads it like this silently loses data:
from flask import Flask, requestapp = Flask(__name__)@app.route("/register", methods=["POST"])def register(): name = request.form["name"] age = int(request.form["age"]) # no ValueError if the user types letters topics = request.form.get("topics") # only gets the first value return f"{name} ({age}) chose: {topics}"
Three bugs hide in this short snippet. First, the browser sends topics=python&topics=flask&topics=django, but .get() returns only the first value, "python" — the other two choices vanish without a warning. Second, every form value is a string, so int(request.form["age"]) raises ValueError and returns a 500 error when the user types letters into the age field. Third, request.form["name"] raises an error when the field is missing, instead of giving you a chance to return a clean error.
The Solution
The correct version uses the three built-in methods of request.form instead of manual reads:
from flask import Flask, requestapp = Flask(__name__)@app.route("/register", methods=["POST"])def register(): name = request.form.get("name", "").strip() age = request.form.get("age", type=int) # None if missing or conversion fails topics = request.form.getlist("topics") # gets EVERY selected value if not name or age is None: return {"error": "Missing name or invalid age"}, 400 return {"name": name, "age": age, "topics": topics}
request.form is a Werkzeug ImmutableMultiDict: one key can hold many values. .get() takes only the first value, while .getlist() takes them all — that single difference decides whether checkbox data gets lost. When no checkbox is selected, getlist() returns an empty list [], so any loop after it still runs safely.
The type=int argument in .get() converts in place: a failed conversion returns None instead of raising, and you only need to check age is None to return a clear 400 error. Note: when conversion fails, Flask returns the default value without an error, so if you want to tell “age not sent” apart from “age sent wrong”, check the raw string request.form.get("age") first.
To see everything that was submitted, use request.form.to_dict(flat=False) — each key maps to a full list of values. And request.form only has data when the client sends a real form format (application/x-www-form-urlencoded or multipart/form-data); if the client sends JSON it is completely empty — then use request.get_json() instead.
Apply It to Your Project
Apply this to your signup form, product filter or survey form right away: replace every request.form[...] with .get() plus a default, every checkbox and multi-select with .getlist(), and every numeric field with .get(..., type=int) plus a None check. Three small changes remove the three most common form-reading bugs. When the form grows and needs cross-field validation or CSRF protection, that’s the time to move to Flask-WTF instead of adding more ifs by hand.
Related Posts
- Flask: Validate Query Strings with request.args.get(type=…) — Block Bad Values at the API Gate in 5 Minutes
- Flask: Read and Validate the JSON Body the Right Way — No More HTML Error Pages from request.get_json() in 5 Minutes
- Flask: validate form an toàn bằng Flask-WTF, khỏi xử lý request.form thủ công
FAQ
How do request.form.get() and request.form[] differ?
.get() returns None or a default when the key is missing, while request.form["name"] raises a 400 error when the key doesn’t exist. Use .get() for optional fields, and [] when you want Flask to return a 400 for a required field automatically.
Why is request.form empty when I send JSON?
Because request.form only parses form-urlencoded or multipart/form-data. A client sending JSON should be read with request.get_json().
What does getlist() return when no checkbox is selected?
An empty list [], not None. So code that iterates the result afterwards needs no None check — just loop normally and it’s safe.
What happens when type=int conversion fails?
Flask returns None (or the default you passed), with no exception. To tell “not sent” apart from “sent wrong”, check the raw string request.form.get("age") before converting.
💬 Have you tried this in your own project? If you hit an error or have a shorter way, leave a comment below — I read them all and will fold the good ideas into the post.
References
- Flask Documentation — Accessing Request Data: https://flask.palletsprojects.com/en/stable/quickstart/#accessing-request-data
- Flask API — flask.Request: https://flask.palletsprojects.com/en/stable/api/#flask.Request
- Werkzeug Documentation — MultiDict: https://werkzeug.palletsprojects.com/en/stable/datastructures/#werkzeug.datastructures.MultiDict
