Reading and Writing request.session
Save and retrieve values like a cart.
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')All lessons in this course
- The Session Framework
- Reading and Writing request.session
- The Messages Framework
- Cookies vs Sessions