current_app e l'oggetto g
Acceda all'app attiva e allo storage per richiesta.
current_app e l'oggetto g è una lezione Flask Academy gratuita su CoddyKit. Questa è la lezione 2 di 4. Puoi leggere la lezione completa qui gratuitamente — poi esercitati direttamente nel browser con un editor di codice integrato e un tutor IA disponibile 24/7. Fa parte del percorso di apprendimento Flask Academy, e i tuoi progressi si sincronizzano tra il web e l'app CoddyKit. Il corso Flask Academy include 4 lezioni in totale.
Parti di questa lezione non sono ancora state tradotte e vengono mostrate in inglese.
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_appWhy 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. 🎉
Domande Frequenti
La lezione «current_app e l'oggetto g» è gratuita?
Sì — il testo completo di «current_app e l'oggetto g» è gratuito qui sul web. Per esercitarvi in modo interattivo (un editor di codice integrato e un tutor IA 24/7) e sbloccare il resto del corso Flask Academy, passa a CoddyKit PRO. Il corso Flask Academy include 4 lezioni in totale.
Cosa imparerò in «current_app e l'oggetto g»?
Acceda all'app attiva e allo storage per richiesta. Eserciti Flask Academy con codice pratico che esegui direttamente nel browser, e un tutor IA 24/7 risponde alle tue domande mentre lavori sulla lezione.
Ho bisogno di esperienza per iniziare Flask Academy?
Non è richiesta alcuna esperienza precedente. Flask Academy su CoddyKit è strutturato per principianti e studenti avanzati, quindi puoi iniziare da qui o dall'inizio e procedere al tuo ritmo. Questa è la lezione 2 di 4.
Quanto tempo richiede la lezione «current_app e l'oggetto g»?
La maggior parte delle lezioni CoddyKit richiede circa 5–10 minuti. Ogni lezione è breve e interattiva, quindi fai progressi costanti e riprendi esattamente da dove hai lasciato su web e app.
Posso scrivere ed eseguire codice in questa lezione Flask Academy?
Sì. Ogni lezione Flask Academy include un editor di codice integrato, quindi scrivi ed esegui codice reale direttamente nel tuo browser e ricevi feedback istantaneo dall'IA — nessuna configurazione locale necessaria.
Tutte le lezioni di questo corso
- App context e request context
- current_app e l'oggetto g
- Context local e sicurezza dei thread
- Attivare un context in script e shell