Reading and Writing request.session
Save and retrieve values like a cart.
Reading and Writing request.session is a free Django Academy lesson on CoddyKit — lesson 2 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.
The Session as a Dict
Think of request.session as a dictionary that follows one visitor. You read and write keys exactly like any Python dict.
Writing a Value
To remember something, just assign a key. Here we save a cart list so the visitor keeps their items across pages.
def add_to_cart(request):
request.session['cart'] = [1, 2, 3]
return redirect('cart')Reading a Value
Read a key back the dictionary way. Using get with a default avoids a crash when the key has not been set yet.
cart = request.session.get('cart', [])Updating Stored Data
To grow a cart, read it, change the Python object, then assign it back so Django notices the update and saves it.
cart = request.session.get('cart', [])
cart.append(7)
request.session['cart'] = cartMark It Modified
When you mutate a nested object in place, set session.modified to True so Django knows it must persist the change.
request.session['cart'].append(7)
request.session.modified = TrueDeleting a Key
Remove a single value with del, just like a normal dictionary, to forget one piece of session state.
del request.session['cart']Checking for a Key
Use the in operator to test whether a value exists before you read it, which keeps your view logic clean.
if 'cart' in request.session:
show_cart()What Can You Store
Session values must be JSON serializable by default. Lists, dicts, strings, and numbers are fine; model instances are not.
Clearing Everything
To wipe all session data for a visitor, call flush. It deletes the data and rotates the session key, handy at logout.
request.session.flush()Set Expiry Per Session
Override the global timeout for one visitor with set_expiry. Passing zero ends the session when the browser closes.
request.session.set_expiry(0)Saves Happen For You
You rarely call save yourself. The middleware writes the session at the end of each request whenever you changed it.
Quick Check
You changed a list already stored in the session in place. What must you do?
Recap
You used request.session like a dict: write, read with get, delete, flush, and set expiry. Remember to flag in-place edits as modified. Next up, the messages framework. 💬
Frequently asked questions
Is the “Reading and Writing request.session” lesson free?
Yes — the full text of “Reading and Writing request.session” 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 “Reading and Writing request.session”?
Save and retrieve values like a cart. 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 2 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Reading and Writing request.session” 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
- The Session Framework
- Reading and Writing request.session
- The Messages Framework
- Cookies vs Sessions