通过 request.form 读取表单字段
提取已提交 HTML 表单中的值
通过 request.form 读取表单字段 是 CoddyKit 上的免费 Flask Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Flask Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Flask Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Forms Send Data in the Body
When an HTML form posts, the field values travel inside the request body, not the URL, so request.args will not find them.
Meet request.form
For posted form fields Flask gives you request.form, a dict-like object keyed by each input name on the page.
name = request.form["name"]The name Attribute Is the Key
A field shows up in request.form under its HTML name attribute, so name="email" becomes request.form["email"].
Use .get for Optional Fields
Just like with args, prefer .get so a field the user left out returns None instead of crashing your view with an error.
phone = request.form.get("phone")Provide a Default
Give .get a fallback and missing optional fields take a clean default, keeping the rest of your logic simple.
role = request.form.get("role", "user")Match the HTTP Method
Form data only arrives on a POST, so your route must allow POST or the browser gets a 405 before your code runs.
@app.route("/signup", methods=["POST"])Form Values Are Strings Too
Every form value is text. Convert ages, prices, or counts yourself, and guard against bad input that will not parse.
age = int(request.form.get("age", 0))Handle Multi-Value Fields
Checkboxes and multi-selects can send a key many times. Use getlist to collect every chosen value into a list.
picks = request.form.getlist("topics")Form vs Args at a Glance
Remember the split: request.args reads the URL query string, while request.form reads fields from the posted body.
values Merges Both Sources
Need either source without caring which? request.values combines args and form into one dict-like view for convenience.
token = request.values.get("token")A Simple Signup View
Putting it together, a signup handler reads name and email from the form and responds once it has them in hand.
@app.route("/signup", methods=["POST"])
def signup():
name = request.form["name"]
return f"Welcome {name}"Quick Check
Check your grip on reading form fields.
Recap: Form Fields
You pulled posted fields from request.form, used .get with defaults, handled multi-value inputs, and saw how it differs from args. 🙌
用 AI 导师学习 Python — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 30
- 课程
- 120
常见问题解答
「通过 request.form 读取表单字段」课时是免费的吗?
是的 — 「通过 request.form 读取表单字段」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Flask Academy 课程的其余内容,请升级到 CoddyKit PRO。 Flask Academy 课程共包含 4 节课。
「通过 request.form 读取表单字段」这节课中我会学到什么?
提取已提交 HTML 表单中的值 你通过在浏览器中直接运行的动手代码来练习 Flask Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Flask Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Flask Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「通过 request.form 读取表单字段」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Flask Academy 课中编写并运行代码吗?
能。每节 Flask Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 通过 request.args 读取查询字符串
- 通过 request.form 读取表单字段
- 通过 request.get_json 读取 JSON 正文
- 标头、Cookie 与客户端 IP