› Forums › Web Development › HarvardX: CS50W – CS50’s Web Programming with Python and JavaScript › CS50W – Lecture 3 – Django › Understanding Django Sessions: Storing User-Specific Data Instead of Global Variables
- This topic is empty.
-
AuthorPosts
-
March 4, 2026 at 4:53 pm #6173
When building web applications with Django, it is common to temporarily store information such as tasks, shopping cart items, or preferences.
At first, beginners often store this data using global variables, but this creates a serious issue: all users share the same data.
Django solves this problem using sessions, which allow the server to store unique data for each user visit.
This post explains the problem with global variables and shows how to use Django sessions to store user-specific data.
1. The Problem with Global Variables
Suppose we are building a simple Django application that allows users to create a list of tasks.
A beginner might write code like this in
views.py:tasks = [] def index(request): return render(request, "tasks/index.html", { "tasks": tasks }) def add(request): task = request.POST["task"] tasks.append(task) return redirect("index")At first glance, this works perfectly.
However, there is a major problem.
The variable:
tasks = []is a global variable stored in the server’s memory.
This means that every user visiting the website shares the same task list.
Example scenario:
User Action What Everyone Sees User A Adds “Buy milk” Buy milk User B Adds “Study Django” Buy milk, Study Django User C Opens site Buy milk, Study Django This is incorrect behavior because each user should have their own list.
2. What Are Sessions?
A session allows the server to store data specific to a particular user.
When a user visits a Django website:
- Django creates a session ID
- The browser stores this ID in a cookie
- The server stores data associated with that session
So instead of one shared list, each user gets their own data.
Example:
User Stored Tasks User A Buy milk User B Study Django User C Empty list
3. Accessing Session Data in Django
Django sessions are accessed using:
request.sessionThis object behaves like a Python dictionary.
Example:
request.session["tasks"]This retrieves the task list belonging to the current user session.
4. Removing the Global Variable
First, delete the global variable:
tasks = []This prevents all users from sharing the same data.
5. Updating the Index View
We now modify the
indexview so that each user has their own task list.def index(request): if "tasks" not in request.session: request.session["tasks"] = [] return render(request, "tasks/index.html", { "tasks": request.session["tasks"] })Explanation:
Step 1: Check if the user already has a session task list.
if "tasks" not in request.sessionStep 2: If not, create one.
request.session["tasks"] = []Step 3: Send that list to the template.
"tasks": request.session["tasks"]Now every user has their own task list stored in their session.
6. Updating the Add Task View
Next, update the function that adds new tasks.
def add(request): task = request.POST["task"] tasks = request.session["tasks"] tasks.append(task) request.session["tasks"] = tasks return redirect("index")What happens here:
- Retrieve the user’s session task list
- Append the new task
- Save the updated list back to the session
This ensures that only that user’s session is updated.
7. Example Folder Structure
A typical Django project might look like this:
project │ ├── tasks │ ├── views.py │ ├── urls.py │ ├── templates │ │ └── tasks │ │ ├── index.html │ │ └── add.html │ ├── project │ ├── settings.py │ ├── urls.py
8. Example Template (index.html)
<h1>Tasks</h1> <ul> {% for task in tasks %} <li>{{ task }}</li> {% endfor %} </ul> <a href="{% url 'add' %}">Add Task</a>
9. Example Add Form
<h2>Add Task</h2> <form action="{% url 'add' %}" method="post"> {% csrf_token %} <input type="text" name="task"> <input type="submit"> </form>
10. Why Sessions Are Important
Sessions are used in many real-world applications.
Feature Stored in Session Login systems user authentication Shopping carts items selected Preferences language/theme Temporary data task lists Examples:
- E-commerce websites store cart items in sessions
- Authentication systems store login information
- Applications store temporary user data
11. Key Takeaway
Global variables share data between all users, which is unsafe for web applications.
Sessions solve this problem by storing data uniquely for each visitor.
By using
request.session, Django allows developers to build applications where each user has their own isolated data.
Final Thought
Understanding sessions is an important step in learning Django because many real web features — such as login systems, shopping carts, and personalized experiences — rely on session-based data storage.
-
AuthorPosts
- You must be logged in to reply to this topic.

