0Pricing
Flask Academy · درس

current_app والكائن g

الوصول إلى التطبيق النشط والتخزين الخاص بكل طلب.

current_app والكائن g درس مجاني في Flask Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Flask Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Flask Academy 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Meet current_app

current_app is a proxy that always points to the app handling the work right now. Import it whenever you need the active application.

from flask import current_app

Why Not Import app?

Importing your app object directly causes circular imports as projects grow. current_app sidesteps that by resolving the app at runtime.

Reading Config

A common use is reading settings: current_app.config gives you the active app's configuration without importing the app module.

key = current_app.config["SECRET_KEY"]

It Needs a Context

current_app only works while an app context is active. Inside a view that is automatic, since the request pushed one for you.

Meet the g Object

g is a simple scratchpad for one request. Attach anything to it and read it back later in the same request.

from flask import g
g.user = "alice"

g Is Per Request

The g object is reset for every request. Data you stash on it never leaks into another visitor's request.

A Typical g Use

Use g to share work across functions in one request, like caching the current user so you fetch it from the database only once.

if "db" not in g:
    g.db = connect()

g Is Not Storage

Do not treat g as a database. It forgets everything the moment the request ends, so use it only for that request's lifetime.

g Lives in the App Context

Surprisingly, g belongs to the app context, not the request context. In practice both exist during a request, so it just works.

Cleaning Up With g

Pair g with teardown handlers to close what you opened, like closing a database connection after the response is sent.

@app.teardown_appcontext
def close(exc):
    g.pop("db", None)

current_app and g Together

They pair nicely: read settings from current_app, then stash request-specific results on g so the rest of the request can reuse them.

Quick Check

Let us pin down what g is for.

Recap

Two handy proxies: current_app finds the active app and its config, while g is a per-request scratchpad you wipe clean each time. 🎉

الأسئلة الشائعة

هل درس «current_app والكائن g» مجاني؟

نعم — نص درس «current_app والكائن g» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Flask Academy، انتقل إلى CoddyKit PRO. تتضمن دورة Flask Academy 4 دروس في المجموع.

ماذا ستتعلم في «current_app والكائن g»؟

الوصول إلى التطبيق النشط والتخزين الخاص بكل طلب. تتمرن على Flask Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Flask Academy؟

لا تُشترط خبرة سابقة. Flask Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.

كم من الوقت يستغرق درس «current_app والكائن g»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Flask Academy هذا؟

نعم. كل درس في Flask Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. سياق التطبيق مقابل سياق الطلب
  2. current_app والكائن g
  3. المحليات السياقية وأمان الخيوط
  4. دفع سياق في البرامج النصية والأصداف
← العودة إلى Flask Academy