URL 前缀与蓝图 url_for
为路由划分命名空间,并跨蓝图创建链接
URL 前缀与蓝图 url_for 是 CoddyKit 上的免费 Flask Academy 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Flask Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Flask Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Group URLs Under a Path
Often every route in a blueprint should sit under one path, like /admin. A url_prefix applies that path to the whole blueprint at once.
Set It at Registration
You pass the prefix when you register the blueprint. Every route inside then lives beneath that shared base path.
app.register_blueprint(admin, url_prefix="/admin")How Prefixes Combine
A route defined as /users under the prefix /admin becomes /admin/users. Flask simply joins the prefix and the route path.
Keep Routes Prefix-Free
Inside the blueprint, write routes without repeating the prefix. The prefix is set once at registration, so your view paths stay short.
@admin.route("/users")
def users():
return "Admin users"Stop Hardcoding Links
Never type URLs as plain strings in templates. url_for builds the correct link from an endpoint name, so links survive path changes.
Endpoints Are Namespaced
With blueprints, an endpoint is blueprint name plus view name. The users view in admin is the endpoint admin.users.
url_for("admin.users")Prefix Is Applied Automatically
url_for already knows the prefix. Calling it for admin.users returns /admin/users, prefix included, with zero extra work.
Linking Within the Same Blueprint
Inside one blueprint you can use a leading dot as a shortcut. url_for(".users") means the users view in the current blueprint.
url_for(".users")Linking Across Blueprints
To link from blog to admin, name both parts fully. Always spell out the blueprint when crossing module boundaries.
url_for("admin.dashboard")Pass Values to Dynamic Routes
Extra keyword arguments to url_for fill dynamic URL parts, so generated links stay correct even for parameterized routes.
url_for("blog.post", post_id=7)Refactor With Confidence
Change a prefix later and every url_for link updates itself. This is why url_for beats hardcoded strings every single time. 🔗
Quick Check
How do you link to a view inside a blueprint?
Recap
A url_prefix mounts every blueprint route under one path, and url_for builds namespaced links like admin.users that update automatically. 🎉
用 AI 导师学习 Python — 免费
在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。
- 课程
- 30
- 课程
- 120
常见问题解答
「URL 前缀与蓝图 url_for」课时是免费的吗?
是的 — 「URL 前缀与蓝图 url_for」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Flask Academy 课程的其余内容,请升级到 CoddyKit PRO。 Flask Academy 课程共包含 4 节课。
「URL 前缀与蓝图 url_for」这节课中我会学到什么?
为路由划分命名空间,并跨蓝图创建链接 你通过在浏览器中直接运行的动手代码来练习 Flask Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Flask Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Flask Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「URL 前缀与蓝图 url_for」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Flask Academy 课中编写并运行代码吗?
能。每节 Flask Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 为什么蓝图胜过一个巨型文件
- 创建并注册蓝图
- URL 前缀与蓝图 url_for
- 蓝图作用域内的模板与静态资源