0Pricing
Django Academy · Lesson

The Session Framework

Store data tied to a single visitor.

The Session Framework is a free Django 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 Django Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Sessions Exist

HTTP forgets you between requests. The session framework lets Django remember each visitor across page loads, so a login or a cart can survive. 🍪

What a Session Stores

A session is a small bag of data tied to one visitor. Django keeps it on the server and gives the browser only a key to find it again.

The Session Cookie

Django sets a cookie named sessionid in the browser. That cookie holds just the session key, never your private data, which stays safe on the server.

It Is Already On

The SessionMiddleware ships enabled in every new project, so sessions work out of the box without any extra setup from you.

MIDDLEWARE = [
    'django.contrib.sessions.middleware.SessionMiddleware',
]

The Sessions App

The django.contrib.sessions app lives in INSTALLED_APPS and creates a table to hold session data after you run migrate.

Where Data Lives

By default Django stores sessions in the database. Each row holds the key, the encoded data, and an expiry date for that visitor.

Choosing a Backend

You pick storage with SESSION_ENGINE. Cache or cached_db backends keep sessions in memory for far faster reads on busy sites.

SESSION_ENGINE = 'django.contrib.sessions.backends.cache'

Accessing the Session

In any view, the session hangs off the request as request.session. It behaves like a normal Python dictionary you can read and write.

def home(request):
    request.session['theme'] = 'dark'
    return render(request, 'home.html')

Sessions Expire

Sessions do not last forever. SESSION_COOKIE_AGE sets the lifetime in seconds, defaulting to two weeks before a visitor is forgotten.

SESSION_COOKIE_AGE = 1209600  # two weeks

Clearing Old Sessions

Expired rows linger in the database. Run clearsessions on a schedule to delete stale data and keep the table tidy.

python manage.py clearsessions

Anonymous and Logged In

Sessions track both anonymous and logged-in visitors. That is how a guest can fill a cart before they ever create an account.

Quick Check

Let us check where session data actually lives.

Recap

You met the session framework: it remembers visitors across requests, stores data server-side, and works automatically through SessionMiddleware. Next you will read and write it. ✅

Frequently asked questions

Is the “The Session Framework” lesson free?

Yes — the full text of “The Session Framework” is free to read here on the web, and the Django 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 Django Academy course, upgrade to CoddyKit PRO.

What will I learn in “The Session Framework”?

Store data tied to a single visitor. You practise Django 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 Django Academy?

No prior experience is required. Django 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 “The Session Framework” 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 Django Academy lesson?

Yes. Every Django 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. The Session Framework
  2. Reading and Writing request.session
  3. The Messages Framework
  4. Cookies vs Sessions
← Back to Django Academy