通过 request.args 读取查询字符串
读取问号后的 URL 参数
通过 request.args 读取查询字符串 是 CoddyKit 上的免费 Flask Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Flask Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Flask Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What Is a Query String
A query string is the part of a URL after the question mark, like ?q=cats&page=2. It carries small bits of optional input. 🔎
Where request Comes From
Flask gives you a global request object that holds everything about the incoming call. Import it once and reach for it inside any view.
from flask import requestMeet request.args
The request.args attribute is a dict-like object holding every key and value parsed from the query string for you.
Read a Value by Key
Treat request.args like a dictionary: index it by the parameter name to pull the matching value straight out of the URL.
term = request.args["q"]Prefer .get to Avoid Crashes
Indexing a missing key raises an error. Use .get instead so an absent parameter returns None rather than a 400.
term = request.args.get("q")Supply a Default Value
Pass a second argument to .get and that value is used when the key is missing, keeping your view safe and predictable.
page = request.args.get("page", "1")Everything Arrives as a String
Query values are always text, even numbers. If you need an int, convert it yourself after reading the raw string.
page = int(request.args.get("page", 1))Coerce With the type Argument
The type keyword on .get converts the value for you and falls back to the default if conversion fails.
page = request.args.get("page", 1, type=int)Repeated Keys With getlist
When one key appears several times, like ?tag=a&tag=b, use getlist to receive every value as a Python list.
tags = request.args.getlist("tag")Loop Over All Parameters
Because request.args behaves like a dict, you can iterate its items to inspect or log every parameter a client sent.
for k, v in request.args.items():
print(k, v)A Realistic Search View
Combine these pieces and your search endpoint reads a term and a page cleanly, with sensible defaults baked in.
@app.route("/search")
def search():
q = request.args.get("q", "")
return f"Searching {q}"Quick Check
Time to lock in how query strings work.
Recap: Query Strings
You read URL parameters through request.args, used .get with defaults, coerced types, and grabbed repeated keys with getlist. Nicely done! 🎉
常见问题解答
「通过 request.args 读取查询字符串」课时是免费的吗?
是的 — 「通过 request.args 读取查询字符串」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Flask Academy 课程的其余内容,请升级到 CoddyKit PRO。 Flask Academy 课程共包含 4 节课。
「通过 request.args 读取查询字符串」这节课中我会学到什么?
读取问号后的 URL 参数 你通过在浏览器中直接运行的动手代码来练习 Flask Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Flask Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Flask Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「通过 request.args 读取查询字符串」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Flask Academy 课中编写并运行代码吗?
能。每节 Flask Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 通过 request.args 读取查询字符串
- 通过 request.form 读取表单字段
- 通过 request.get_json 读取 JSON 正文
- 标头、Cookie 与客户端 IP