类型转换器:int、float、string、path
使用内置转换器限制 URL 片段
类型转换器:int、float、string、path 是 CoddyKit 上的免费 Flask Academy 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Flask Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Flask Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Strings Are Not Always Enough
Captured values default to strings, but /post/42 should give a number, not text. A converter tells Flask what type to expect in that segment.
Converter Syntax
Put the converter name before the variable with a colon. The pattern is <converter:name>, and Flask applies it before calling your view.
@app.route('/post/<int:post_id>')
def show(post_id):
return str(post_id + 1)The int Converter
Use int to capture whole numbers. /post/42 hands your view the integer 42, so you can do math without calling int() yourself. 🔢
@app.route('/page/<int:n>')
def page(n):
return f'Page {n}'int Rejects Non-Numbers
A converter also filters URLs. /page/abc will not match an int route, so Flask returns 404 instead of crashing inside your function.
The float Converter
Use float for decimal numbers like prices or ratings. /rate/4.5 gives you the float 4.5, ready for arithmetic right away.
@app.route('/rate/<float:score>')
def rate(score):
return str(score * 2)The string Converter
string is the default: it accepts any text but stops at a slash. You rarely write it explicitly since plain <name> already behaves this way.
@app.route('/tag/<string:label>')
def tag(label):
return labelThe path Converter
Use path when the value can contain slashes, like a folder route. Unlike string, it captures /docs/intro/setup as one whole value.
@app.route('/files/<path:filepath>')
def serve(filepath):
return filepathPick the Right Tool
Match the converter to your data: int for ids, float for decimals, string for words, path for slashed routes. The right choice prevents bad input early.
Converters Validate for You
By rejecting mismatched URLs with a 404, converters act as a first line of validation. Your view only runs when the type already fits.
Combine in One Route
You can mix converters across segments. Here an int id and a string tab live in the same path, each typed independently.
@app.route('/user/<int:uid>/<tab>')
def view(uid, tab):
return f'{uid}:{tab}'Negatives and Edge Cases
The built-in int converter matches only non-negative whole numbers by default. For minus signs or stricter rules, you would write a custom converter later.
Quick Check
Which converter lets a captured value include slashes, like docs/intro/setup?
Recap: You Typed Your URLs
Converters give captured values a type and reject bad input with a 404. Remember int, float, string, and path. Next: building URLs safely. 🧭
常见问题解答
「类型转换器:int、float、string、path」课时是免费的吗?
是的 — 「类型转换器:int、float、string、path」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Flask Academy 课程的其余内容,请升级到 CoddyKit PRO。 Flask Academy 课程共包含 4 节课。
「类型转换器:int、float、string、path」这节课中我会学到什么?
使用内置转换器限制 URL 片段 你通过在浏览器中直接运行的动手代码来练习 Flask Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Flask Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Flask Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「类型转换器:int、float、string、path」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Flask Academy 课中编写并运行代码吗?
能。每节 Flask Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 从路径中捕获变量
- 类型转换器:int、float、string、path
- 使用 url_for 构建 URL
- 末尾斜杠与重定向行为