0Pricing
Flask Academy · レッスン

1つのアプリに複数のルートを定義する

複数のページを追加し、ルートの衝突を避けます

「1つのアプリに複数のルートを定義する」はCoddyKit上の無料Flask Academyレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはFlask Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Flask Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Many Pages, One App

A real site has many pages. You add each one with its own route and view function, all living inside the same app file.

Stack the Decorators

Just write more @app.route blocks. Each pairs a unique path with its own function, and Flask keeps track of every one.

@app.route("/home")
def home():
    return "Home"

Add Another Page

Below the first route, add a second for a new path like /about. Two routes, two functions, two pages your users can visit.

@app.route("/about")
def about():
    return "About"

Each Path Is Unique

Two routes must not share the same path. If they do, Flask cannot tell which view to call and raises an error at startup.

The Collision Error

Reusing a path triggers a collision. Flask warns that the endpoint is already registered, so give every route a distinct URL.

Function Names Must Differ

Each view also needs a unique function name. Two functions named home overwrite each other, and only the last one survives.

Endpoints Behind the Scenes

Flask stores each route under an endpoint name, which defaults to the function name. That name is how Flask refers to the route internally.

Group Related Routes

Keep related pages near each other in the file. Good grouping makes a growing app easier to read and to maintain over time.

Order Does Not Decide Matching

Flask picks a route by the requested path, not by source order. So the position of a route in the file does not change which page loads.

One File Has Limits

Dozens of routes in one file get unwieldy. Later you will split them into blueprints, but for now a single file is perfectly fine.

A Small Multi-Page App

Here is the shape: distinct paths, distinct names, one app. Add a contact route the same way and your site keeps growing.

@app.route("/contact")
def contact():
    return "Contact"

Quick Check

Spot what causes a route collision.

Recap

You build many pages by stacking routes, each with a unique path and name. Keep them tidy now, and blueprints will scale you later. 🧭

よくある質問

「1つのアプリに複数のルートを定義する」レッスンは無料ですか?

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

「1つのアプリに複数のルートを定義する」で何を学びますか?

複数のページを追加し、ルートの衝突を避けます ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「1つのアプリに複数のルートを定義する」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. @app.routeデコレーター
  2. リクエストがレスポンスになるまで
  3. 文字列、HTML、ステータスコードを返す
  4. 1つのアプリに複数のルートを定義する
← Flask Academyに戻る