0Pricing
Flask Academy · レッスン

url_forでURLを構築する

ハードコードしたパスではなくエンドポイント名からリンクを生成します

「url_forでURLを構築する」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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を構築する」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。

「url_forでURLを構築する」で何を学びますか?

ハードコードしたパスではなくエンドポイント名からリンクを生成します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応の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に戻る