0Pricing
Flask Academy · Lesson

App Context vs Request Context

Two contexts and what lives in each.

App Context vs Request Context is a free Flask Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Flask Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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.path

Why 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. 🎉

Frequently asked questions

Is the “App Context vs Request Context” lesson free?

Yes — the full text of “App Context vs Request Context” is free to read here on the web, and the Flask Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Flask Academy course, upgrade to CoddyKit PRO.

What will I learn in “App Context vs Request Context”?

Two contexts and what lives in each. You practise Flask Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Flask Academy?

No prior experience is required. Flask Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “App Context vs Request Context” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Flask Academy lesson?

Yes. Every Flask Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. App Context vs Request Context
  2. current_app and the g Object
  3. Context Locals and Thread Safety
  4. Push a Context in Scripts and Shells
← Back to Flask Academy