تهيئة الامتدادات باستخدام init_app
أجّل ربط الامتدادات حتى إنشاء التطبيق.
تهيئة الامتدادات باستخدام init_app درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 3 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في 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 yetBind 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 appWhy 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 dbA 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/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.
ماذا ستتعلم في «تهيئة الامتدادات باستخدام init_app»؟
أجّل ربط الامتدادات حتى إنشاء التطبيق. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟
لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 3 من أصل 4.
كم من الوقت يستغرق درس «تهيئة الامتدادات باستخدام init_app»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟
نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- مشكلات كائن app العام
- كتابة دالة create_app
- تهيئة الامتدادات باستخدام init_app
- تسجيل Blueprints في المصنع