App Context und Request Context
Zwei Kontexte und die darin enthaltenen Objekte.
App Context und Request Context ist eine kostenlose Flask Academy-Lektion auf CoddyKit. Dies ist Lektion 1 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Flask Academy-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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. 🎉
Häufig gestellte Fragen
Ist die Lektion „App Context und Request Context“ kostenlos?
Ja — der vollständige Text von „App Context und Request Context“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Flask Academy-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Flask Academy-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „App Context und Request Context“?
Zwei Kontexte und die darin enthaltenen Objekte. Du übst Flask Academy mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Flask Academy zu starten?
Keine Vorkenntnisse erforderlich. Flask Academy auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 1 von 4.
Wie lange dauert die Lektion „App Context und Request Context“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Flask Academy-Lektion Code schreiben und ausführen?
Ja. Jede Flask Academy-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- App Context und Request Context
- current_app und das g-Objekt
- Context Locals und Thread-Sicherheit
- Einen Context in Skripten und Shells pushen