سياق التطبيق مقابل سياق الطلب
تعرّف إلى السياقين وما يوجد في كل منهما.
سياق التطبيق مقابل سياق الطلب درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 1 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Two Kinds of Context
Flask runs your code inside a context, a temporary bubble holding the data your view needs. There are two flavors: an app context and a request context.
The App Context
The application context tracks which app is active right now. It exists whenever Flask is doing work, even outside a real web request.
The Request Context
The request context wraps a single incoming request. It holds the URL, headers, and form data for that one visitor only.
Request Pushes App Too
When a request arrives, Flask pushes a request context, and an app context rides along automatically. So inside a view, both are always active.
Who Lives in the App Context
The app context gives you current_app and the g object. Use them for config, the database handle, and other per-request shared data.
from flask import current_app
print(current_app.name)Who Lives in the Request Context
The request context gives you request and session. These describe the visitor: their path, query string, posted form, and cookies.
from flask import request
path = request.pathWhy Split Them?
Splitting contexts means app-level work, like a startup script, can use current_app without pretending a fake web request exists.
Outside Any Context
Touch request with no active context and Flask raises a runtime error. The proxy has nothing real to point at yet.
Contexts Are Stacked
Flask keeps contexts on a stack. Each is pushed when work starts and popped when it ends, so the right data is always on top.
Lifespan of a Context
A request context lives only for that one request. Once the response is sent, Flask pops both contexts and they vanish.
Mental Model
Think of it this way: the app context answers which app, and the request context answers which visitor. Inside a view you have both at once.
Quick Check
Let us sort the two contexts apart.
Recap
Two bubbles, two jobs: the app context says which app, the request context says which visitor. A request pushes both, so views always have each. 🎉
الأسئلة الشائعة
هل درس «سياق التطبيق مقابل سياق الطلب» مجاني؟
نعم — نص درس «سياق التطبيق مقابل سياق الطلب» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.
ماذا ستتعلم في «سياق التطبيق مقابل سياق الطلب»؟
تعرّف إلى السياقين وما يوجد في كل منهما. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟
لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 1 من أصل 4.
كم من الوقت يستغرق درس «سياق التطبيق مقابل سياق الطلب»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟
نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- سياق التطبيق مقابل سياق الطلب
- current_app والكائن g
- المحليات السياقية وأمان الخيوط
- دفع سياق في البرامج النصية والأصداف