0Pricing
Flask Academy · レッスン

init_appで拡張機能を初期化する

アプリ作成まで拡張機能のバインドを遅らせます

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

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

The Extension Dilemma

Extensions like SQLAlchemy used to take the app right away. Inside a factory that does not work, because the app does not exist at import time yet. 🤔

Create Without an App

The fix is two steps. First, create the extension object at module level with no app passed in. It stays unbound for now.

from flask_sqlalchemy import SQLAlchemy
db = SQLAlchemy()  # no app yet

Bind It Later

Second, call init_app inside the factory once the app is built. This binds the extension to that specific instance.

def create_app():
    app = Flask(__name__)
    db.init_app(app)
    return app

Why Two Steps

The extension object can be imported anywhere as a global, while the actual binding waits until the factory chooses a config. Best of both worlds.

Importable Everywhere

Because db lives at module level, your models and views import it freely without touching the app, avoiding circular imports.

from app.extensions import db

A Tidy extensions Module

A common pattern keeps all unbound extensions in one extensions.py file, then the factory imports and inits each one.

db = SQLAlchemy()
migrate = Migrate()
login = LoginManager()

Init Many at Once

Inside create_app you simply call init_app on each extension, passing the same fresh app to all of them.

db.init_app(app)
migrate.init_app(app, db)
login.init_app(app)

Per-App Binding

Because binding happens in the factory, two different apps can each init_app the same extension class independently. Great for tests.

Config Is Read at Init

Extensions read settings like the database URI during init_app, so set those config values before you bind them.

app.config["SQLALCHEMY_DATABASE_URI"] = uri
db.init_app(app)

Use It Within a Context

After init_app, the extension needs an app context to actually run queries, since it must know which app it is talking to.

with app.app_context():
    db.create_all()

The Golden Rule

Create the extension unbound at import, then call init_app in the factory. That single split unlocks the whole pattern. 🔑

Quick Check

Why create an extension object without an app, then call init_app?

Recap

You learned the two-step move: build extensions unbound, then call init_app in the factory. Importable globals plus per-app binding, with no circular imports. 🌟

よくある質問

「init_appで拡張機能を初期化する」レッスンは無料ですか?

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

「init_appで拡張機能を初期化する」で何を学びますか?

アプリ作成まで拡張機能のバインドを遅らせます ブラウザで直接実行するハンズオンコードでFlask Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

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

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

「init_appで拡張機能を初期化する」レッスンにはどのくらい時間がかかりますか?

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

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

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

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

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