Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Making a simple counter app using request.session in Django
In this article, we are going to see how to make a super simple counter app using Django sessions. By clicking a button, the counter increments and persists even after closing the browser tab. This demonstrates how to use request.session for state management in Django.
Setting Up URLs
In urls.py, add the following lines ?
from django.urls import path
from . import views
urlpatterns = [
path('', views.counterView, name='counter'),
]
Here we set up the counter view on the home URL.
Creating the View
In views.py, add the following lines ?
from django.shortcuts import render
def counterView(request):
if request.method == "POST" and 'count' in request.POST:
try:
request.session['count'] += 1
except KeyError:
request.session['count'] = 1
elif request.method == 'POST' and 'reset' in request.POST:
request.session['count'] = 0
return render(request, 'counter.html')
This view handles POST requests for counting and resetting. The counter value is stored in request.session['count'] which persists across browser sessions.
Creating the Template
Create a templates folder in your app directory and create counter.html ?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Counter</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css"
rel="stylesheet">
</head>
<body>
<style>
body {
background-color: palevioletred;
}
.counter form .count {
border: none;
outline: none;
background-color: black;
color: white;
}
.counter form .reset {
border: none;
outline: none;
background-color: rgb(50, 181, 204);
}
</style>
<div class="container counter text-center" style="margin-top: 150px;">
<h1 class="display-1 text-white">
{% if request.session.count %}
{{ request.session.count }}
{% else %}
0
{% endif %}
</h1>
<br><br>
<form method="post">
{% csrf_token %}
<button name="count" class="count px-5 py-3 text-white shadow-lg">
Count
</button>
</form>
<br><br>
<form method="post">
{% csrf_token %}
<button name="reset" class="reset px-5 py-3 text-white shadow-lg">
Reset
</button>
</form>
</div>
</body>
</html>
How Sessions Work
Django sessions store data on the server and use cookies to identify users. When you set request.session['count'], Django automatically handles:
- Storage: Data is saved in the database or cache
- Retrieval: Data persists across requests
- Cleanup: Sessions expire based on settings
Output
Clicking the Count button increments the number by 1, while the Reset button sets the counter back to 0. The value persists even after closing the browser tab.
Conclusion
Django sessions provide an easy way to store user-specific data across requests. This counter app demonstrates basic session usage and forms the foundation for more complex stateful web applications.
