Django page counter using django session

Jan. 27, 2023

3 min read

In today's article, I will tell you how you can create a page counter using a django session. With the help of this, you can count the user's sessions on your website and you can use it as a counter. You can use it in many ways. So I will tell you without delay how you will become a page counter.

Your system should have Django installed, for that you will run this command:

pip install django

Then you have to create a Django project, and for that, you will run this command:

django-admin startproject pagecounter

pagecounter is the name of my project, you can give it whatever you want. this will create your project

Now you have to create your app by going to your project directory (cd pagecounter), for that you have to run this command:

python startapp mycount

You have to mention your app in the project setting, for this, you have to go to

# Application definition

    "mycount",  # new

Now you have to write your logic in

from django.shortcuts import render, redirect
from django.contrib.sessions.models import Session

# Create your views here.

# this for page count
def home(request):
    count = request.session.get("count", -1)
    newcount = count + 1
    request.session["count"] = newcount
    return render(request, "home.html", {"count": newcount})

# this for reset
def reset(request):
    reset = Session.objects.all()
    return redirect("/")

Now you have to create your URLs. You have to create a file named inside your app.

from django.urls import path
from . import views

urlpatterns = [
    path("", views.home, name="home"),
    path("reset/", views.reset, name="reset"),

These URLs have to be included inside the URLs of your project. Goto project's and include it.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path("", include("mycount.urls")), # new

Now you have to create template. You have to go inside your app and create a directory named templates and create a file named home.html inside it.


<!DOCTYPE html>
<html lang="en">
 <meta charset="UTF-8">
 <meta name="viewport" content="width=device-width, initial-scale=1.0">
 <!-- <meta http-equiv="refresh" content="1"> -->
 <link rel="stylesheet" href="" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
   font-family: 'Pacifico', cursive;
  body,html {
   height: 100%;
 <title>Page Counter</title>
<body class="bg-dark">
  <div class="container d-flex align-items-center justify-content-center h-100">
    <h1 class="text-warning display-2">
      Page Count: <span class="text-white">{{count}}</span><br>
      <a align="center" class="btn btn-outline-warning" href="{% url 'reset' %}">Reset</a>

 <script src="" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
 <script src="" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
 <script src="" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>

Now you have to create session table in your database for that you have to migrate

python migrate

Now your project is ready, run it

python runserver


If you have any issues then you can comment or you can direct message me this is my profile Abdulla Fajal

If you want to learn Django, you can join our Django Beginner to Advanced free course, we will bring more chapters soon.


django projects page-counter count Appreciate you stopping by my post! 😊

Add a comment

Note: If you use these tags, write your text inside the HTML tag.
Login Required
Author's profile
Profile Image

Abdulla Fajal

Django Developer

With '' under my care, I, Abdulla Fajal, graciously invite your insights and suggestions, as we endeavour to craft an exquisite online experience together.