0Pricing
Flask Academy · レッスン

@app.routeデコレーター

URLパスをビュー関数に結び付けます

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

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

What a Route Is

A route connects a URL path to a Python function. When someone visits that path, Flask runs your function and sends back whatever it returns.

Meet the Decorator

You attach a route using @app.route, a decorator written on the line just above your function. It tells Flask which path this function should handle.

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

View Functions

The function below a route is called a view function. Its job is simple: build a response for one specific URL and return it.

The Path Argument

The string you pass, like "/about", is the URL path. It always starts with a slash and tells Flask exactly where this page lives.

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

The Root Path

A single slash, "/", is the root path. It is your home page, the page people see when they open your site with no extra path.

Returning a Response

Whatever your view function returns becomes the page body. A plain string is the simplest response Flask can send to a browser.

@app.route("/hi")
def hi():
    return "Hi there"

Why a Decorator?

The @ symbol means decorator. It registers your function with Flask without you calling anything by hand, so Flask knows about the route at startup.

Function Names Are Yours

The function name does not have to match the URL. Pick clear names like contact or profile, since Flask uses them later to build links.

One Decorator, One Path

Each @app.route binds one path to one function. Want another page? Write another decorator and another function below it.

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

Order Inside the File

You can declare routes in any order in your file. Flask matches by the requested path, not by which function you wrote first.

Paths Are Case Sensitive

Route paths are case sensitive after the host. "/About" and "/about" are different URLs, so keep your paths lowercase and consistent.

Quick Check

Let us make sure the decorator clicked.

Recap

You now wire pages with @app.route: a path string, a view function, and a returned response. That trio is the heart of every Flask app. 🎉

よくある質問

「@app.routeデコレーター」レッスンは無料ですか?

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

「@app.routeデコレーター」で何を学びますか?

URLパスをビュー関数に結び付けます ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「@app.routeデコレーター」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

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