إنشاء Blueprint وتسجيله
عرّف Blueprint وألحقه بالتطبيق.
إنشاء Blueprint وتسجيله درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Two Steps to a Blueprint
Working with a blueprint is two moves: first you define it in its own module, then you register it on the app. Let us walk through both.
Import the Blueprint Class
Everything starts with one import. The Blueprint class comes straight from flask, just like the Flask app object does.
from flask import BlueprintCreate the Blueprint Object
You build a blueprint by giving it a name and import_name. The name uniquely identifies this blueprint inside your whole app.
blog = Blueprint("blog", __name__)What __name__ Does
That second argument, __name__, tells the blueprint where it lives so Flask can later find its templates and static files.
Routes on the Blueprint
Inside the module you attach views with @blog.route instead of @app.route. Same idea, but bound to the blueprint.
@blog.route("/posts")
def posts():
return "All posts"Still Dormant for Now
Defining routes is not enough. Until you register the blueprint, none of its URLs respond. The blueprint is inert on its own.
Import It in the App
Over in your app setup, you import the blueprint object from its module so the app can take ownership of it.
from blog.routes import blogRegister on the App
One call wires everything up. register_blueprint copies the blueprint routes onto the app and makes them live.
app.register_blueprint(blog)Order of Operations
Create the app, import the blueprint, then register it. Get this sequence right and your feature routes spring to life. ✅
Endpoint Names Get Namespaced
A view named posts inside the blog blueprint is referenced as blog.posts. The blueprint name becomes a tidy prefix for endpoints.
Avoiding Circular Imports
Keep the blueprint in its own file and import it where you build the app. This structure sidesteps painful circular import errors.
Quick Check
Which call actually makes a blueprint active?
Recap
Define a Blueprint with a name and __name__, add routes with @bp.route, then call register_blueprint on the app to make them live. 🎉
تعلم Python مع معلم ذكاء اصطناعي — مجانًا
اكتب وقم بتشغيل أكوادك الفعلية في المتصفح، واحصل على مساعدة فورية من معلم ذكاء اصطناعي متاح 24/7، واستمر من حيث توقفت على الويب أو في التطبيق.
- الدورات
- 30
- الدروس
- 120
الأسئلة الشائعة
هل درس «إنشاء Blueprint وتسجيله» مجاني؟
نعم — نص درس «إنشاء Blueprint وتسجيله» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.
ماذا ستتعلم في «إنشاء Blueprint وتسجيله»؟
عرّف Blueprint وألحقه بالتطبيق. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟
لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «إنشاء Blueprint وتسجيله»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟
نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- لماذا تتفوق Blueprints على ملف واحد ضخم
- إنشاء Blueprint وتسجيله
- بادئات URL وBlueprint url_for
- القوالب والملفات الثابتة الخاصة بـ Blueprint