0Pricing
Flask Academy · レッスン

URLプレフィックスとBlueprintのurl_for

ルートを名前空間で整理し、Blueprint間でリンクします

「URLプレフィックスとBlueprintのurl_for」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応の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. 🎉

よくある質問

「URLプレフィックスとBlueprintのurl_for」レッスンは無料ですか?

はい。「URLプレフィックスとBlueprintのurl_for」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Flask Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Flask Academyコースには全4レッスンが含まれています。

「URLプレフィックスとBlueprintのurl_for」で何を学びますか?

ルートを名前空間で整理し、Blueprint間でリンクします ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Flask Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのFlask Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「URLプレフィックスとBlueprintのurl_for」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このFlask Academyレッスンでコードを書いて実行できますか?

はい。すべてのFlask Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. 巨大な1ファイル構成よりBlueprintが優れている理由
  2. Blueprintを作成して登録する
  3. URLプレフィックスとBlueprintのurl_for
  4. Blueprint専用のテンプレートと静的ファイル
← Flask Academyに戻る