0Pricing
Django Academy · Lesson

Cookies vs Sessions

Choose the right place to store state.

Cookies vs Sessions is a free Django Academy lesson on CoddyKit — lesson 4 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.

Two Ways to Remember

Both cookies and sessions help your app remember a visitor, but they store data in very different places. Knowing which to pick matters. 🤔

What a Cookie Is

A cookie is a small piece of text the browser keeps and sends back on every request to your site. The data lives on the client.

Setting a Cookie

You set a cookie on the response object with set_cookie, giving it a name, a value, and optionally a lifetime.

response = HttpResponse('Hi')
response.set_cookie('lang', 'en', max_age=3600)

Reading a Cookie

Cookies arrive in request.COOKIES, a simple dictionary. Read your value back by its key on the next request.

lang = request.COOKIES.get('lang', 'en')

Where Session Data Lives

A session keeps the real data on the server and sends the browser only a key. The visitor never sees the contents.

Cookies Are Visible

Because cookies sit in the browser, the user can read and edit them. So never store secrets like prices or user roles there.

Sessions Hide the Data

Session data stays server-side, so it is the right home for anything sensitive or anything the user must not tamper with.

Size Limits

Cookies are tiny, capped near 4KB each, while sessions can hold much larger structures since they live in your database or cache.

Protect Your Cookies

Add HttpOnly so JavaScript cannot read a cookie, and Secure so it is only sent over HTTPS. Django sets these for sessionid by default.

response.set_cookie('t', '1', httponly=True, secure=True)

When to Use a Cookie

Reach for a plain cookie for small, non-secret preferences like a theme or a chosen language that is fine for the user to see.

When to Use a Session

Use a session for login state, shopping carts, or anything private. It safely backs the cookie with trusted server storage.

Quick Check

You need to store a value the user must never read or change. What fits best?

Recap

You compared cookies and sessions: cookies store small, visible data on the client, sessions keep larger, private data on the server. Pick by sensitivity and size. 🎉

Frequently asked questions

Is the “Cookies vs Sessions” lesson free?

Yes — the full text of “Cookies vs Sessions” 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 “Cookies vs Sessions”?

Choose the right place to store state. 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 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Cookies vs Sessions” 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