0Pricing
Flask Academy · 课时

使用 url_for 构建 URL

根据端点名称生成链接,而不是硬编码路径

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

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

Hardcoded URLs Are Fragile

Typing "/user/alice" by hand breaks the day you rename a route. Flask offers url_for to build links from your view names instead.

Build by Endpoint Name

url_for takes the view function name and returns its path. Change the route string later and every generated link updates automatically. 🔗

from flask import url_for
url_for('home')

The Endpoint Is the Function Name

By default the endpoint is the view function's name, not its URL path. So def home() is referenced as url_for('home').

@app.route('/')
def home():
    return 'Hi'

Pass Dynamic Values

For routes with captured parts, supply them as keyword arguments. url_for drops each value into the matching angle-bracket slot.

url_for('profile', name='alice')
# -> '/user/alice'

Extra Args Become Query Strings

Arguments that do not fit a route slot are added as a query string. So passing page=2 appends ?page=2 to the generated URL.

url_for('search', q='cats', page=2)
# -> '/search?q=cats&page=2'

Use It Inside Templates

Templates call url_for too, so your HTML links never go stale. It is the standard way to write hrefs in Jinja2 pages.

<a href="{{ url_for('home') }}">Home</a>

Refactor Without Fear

Because links derive from endpoint names, you can rename a URL path freely. Every url_for call keeps working, so refactors stay safe.

Redirect Pairs Well with It

Combine url_for with redirect to send users to a named view. You get a clean, refactor-proof redirect without writing the path by hand.

from flask import redirect
redirect(url_for('home'))

Absolute URLs When Needed

Pass _external=True to get a full http://host/path link. This is handy for emails or anywhere a relative path will not do.

url_for('home', _external=True)

It Knows the static Folder

url_for also builds links to assets via the static endpoint. That keeps CSS and image paths correct even if your app mounts elsewhere.

url_for('static', filename='app.css')

A Habit Worth Forming

Make url_for your default for every internal link. Hardcoded strings creep in and rot; generated URLs stay correct as your app evolves.

Quick Check

You call url_for('profile', name='alice', page=2). Where does page=2 end up?

Recap: You Built URLs Smartly

url_for turns endpoint names into paths, fills route variables, and tacks extras onto the query string. Next: trailing slashes. 🧩

常见问题解答

「使用 url_for 构建 URL」课时是免费的吗?

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

「使用 url_for 构建 URL」这节课中我会学到什么?

根据端点名称生成链接,而不是硬编码路径 你通过在浏览器中直接运行的动手代码来练习 Flask Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Flask Academy 需要有经验吗?

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

「使用 url_for 构建 URL」课时需要多长时间?

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

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

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

此课程中的所有课时

  1. 从路径中捕获变量
  2. 类型转换器:int、float、string、path
  3. 使用 url_for 构建 URL
  4. 末尾斜杠与重定向行为
← 返回 Flask Academy