0Pricing
Flask Academy · レッスン

ファクトリでBlueprintを登録する

アプリの組み立て時にすべてのBlueprintを接続します

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

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

The Final Assembly

With the app built and extensions bound, the last factory job is wiring up your routes. You do that by registering blueprints. 🧩

A Quick Blueprint Recall

A blueprint groups related routes into a reusable module. The factory is exactly where you attach those groups to the app.

from flask import Blueprint
main = Blueprint("main", __name__)

Register Inside create_app

Import each blueprint and call app.register_blueprint on the fresh app, all within the factory body.

def create_app():
    app = Flask(__name__)
    from .main import main
    app.register_blueprint(main)
    return app

Import Inside the Function

Notice the blueprint import sits inside create_app. This deferred import is the trick that breaks circular import chains.

Add a URL Prefix

Give a blueprint its own path namespace with url_prefix at registration time, keeping related routes neatly grouped.

app.register_blueprint(api, url_prefix="/api")

Register Several

Real apps have many blueprints. Register each one in the factory, and each contributes its own slice of routes to the app.

app.register_blueprint(auth)
app.register_blueprint(blog)
app.register_blueprint(api, url_prefix="/api")

Order Is Predictable

Because registration happens in one place, the setup order is obvious and easy to read: config, extensions, then blueprints.

Everything in One View

The factory reads like a recipe: make the app, load config, init extensions, register blueprints, return. One glance shows the whole wiring. 👀

Endpoints Get Namespaced

Blueprint endpoints are prefixed by the blueprint name, so url_for uses blueprint.view to link across modules without clashes.

url_for("main.home")

Fresh Wiring Every Time

Each call to create_app registers blueprints onto a brand new app, so tests and prod never share route state.

The Complete Pattern

Config, extensions via init_app, and blueprints via register_blueprint, all inside the factory. That is the full application factory pattern.

Quick Check

Where should blueprints be registered in the factory pattern?

Recap

You finished the pattern: register blueprints inside create_app with deferred imports and optional prefixes. Config, extensions, blueprints, return. 🏁

よくある質問

「ファクトリでBlueprintを登録する」レッスンは無料ですか?

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

「ファクトリでBlueprintを登録する」で何を学びますか?

アプリの組み立て時にすべてのBlueprintを接続します ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「ファクトリでBlueprintを登録する」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

  1. グローバルなappオブジェクトの問題
  2. create_app関数を書く
  3. init_appで拡張機能を初期化する
  4. ファクトリでBlueprintを登録する
← Flask Academyに戻る