0Pricing
Flask Academy · Lektion

Extensions mit init_app initialisieren

Verschieben Sie die Bindung der Extension bis zur App-Erstellung.

Extensions mit init_app initialisieren ist eine kostenlose Flask Academy-Lektion auf CoddyKit. Dies ist Lektion 3 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Flask Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.

Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.

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. 🌟

Häufig gestellte Fragen

Ist die Lektion „Extensions mit init_app initialisieren“ kostenlos?

Ja — der vollständige Text von „Extensions mit init_app initialisieren“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Flask Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.

Was lerne ich in „Extensions mit init_app initialisieren“?

Verschieben Sie die Bindung der Extension bis zur App-Erstellung. Du übst Flask Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.

Brauche ich Erfahrung, um Flask Academy zu starten?

Keine Vorkenntnisse erforderlich. Flask Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 3 von 4.

Wie lange dauert die Lektion „Extensions mit init_app initialisieren“?

Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.

Kann ich in dieser Flask Academy-Lektion Code schreiben und ausführen?

Ja. Jede Flask Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.

Alle Lektionen in diesem Kurs

  1. Probleme mit einem globalen app-Objekt
  2. Eine create_app-Funktion schreiben
  3. Extensions mit init_app initialisieren
  4. Blueprints in der Factory registrieren
← Zurück zu Flask Academy