0Pricing
Flask Academy · 课时

读取查询参数并设置默认值

使用 args.get、备用值和类型转换

读取查询参数并设置默认值 是 CoddyKit 上的免费 Flask Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Flask Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Flask Academy 课程共包含 4 节课。

本课时的部分内容尚未翻译,以英文显示。

Query Params Recap

Query parameters ride in the URL after a question mark, like ?page=2&sort=new. They let clients pass optional tweaks to a request. 🔧

They Live in request.args

Flask parses the query string into request.args, a dict-like object you can read inside any view function.

from flask import request
args = request.args

Bracket Access Is Risky

Reading a key with brackets crashes with a 400 when that key is missing. Real users will eventually drop a parameter you expected.

page = request.args["page"]

Reach for .get

The .get method returns None instead of raising when a key is absent, so your endpoint stays alive through messy input.

page = request.args.get("page")

Give It a Default

Pass a second argument and .get returns that fallback when the key is missing, so the view always has a usable value.

page = request.args.get("page", "1")

Everything Is a String

Query values always arrive as text. A page of 2 reads as the string two, so doing math on it directly will surprise you.

request.args.get("page") + 1  # error

Coerce With type

The type keyword converts the value and quietly returns the default if conversion fails, giving you a clean integer.

page = request.args.get("page", 1, type=int)

Bad Input Falls Back

If a client sends ?page=oops, the type=int conversion fails and .get hands back your default rather than blowing up.

request.args.get("page", 1, type=int)  # 1

Booleans Need Care

There is no real bool type here. Compare the raw string yourself to read a flag, since any non empty text is truthy in Python.

active = request.args.get("active") == "1"

Combine Several Params

Read each parameter with its own default. Together they shape a filtered list view without ever crashing on absent keys.

page = request.args.get("page", 1, type=int)
sort = request.args.get("sort", "new")

A Defensive List View

This products route never errors: missing or invalid query params simply collapse into safe, sensible defaults.

@app.route("/products")
def products():
    page = request.args.get("page", 1, type=int)
    return f"Page {page}"

Quick Check

One question to seal in safe query reading.

Recap: Safe Defaults

You read query params with .get, supplied defaults, coerced types, and handled bad input gracefully. Your views stay crash free. 🎉

常见问题解答

「读取查询参数并设置默认值」课时是免费的吗?

是的 — 「读取查询参数并设置默认值」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Flask Academy 课程的其余内容,请升级到 CoddyKit PRO。 Flask Academy 课程共包含 4 节课。

「读取查询参数并设置默认值」这节课中我会学到什么?

使用 args.get、备用值和类型转换 你通过在浏览器中直接运行的动手代码来练习 Flask Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Flask Academy 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Flask Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。

「读取查询参数并设置默认值」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Flask Academy 课中编写并运行代码吗?

能。每节 Flask Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 构建一个提交数据的 HTML 表单
  2. 读取查询参数并设置默认值
  3. 手动验证必填字段
  4. 提交后重定向(PRG 模式)
← 返回 Flask Academy