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 manage.py startapp mycount
You have to mention your app in the project setting, for this, you have to go to settings.py
# Application definition
INSTALLED_APPS = [
"django.contrib.admin",
"django.contrib.auth",
"django.contrib.contenttypes",
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"mycount", # new
]
Now you have to write your logic in views.py
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()
reset.delete()
return redirect("/")
Now you have to create your URLs. You have to create a file named urls.py
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 urls.py
and include it.
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path("admin/", admin.site.urls),
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.
mycount/templates/home.html
<!DOCTYPE html>
<html lang="en">
<head>
<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="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.min.css" integrity="sha384-9aIt2nRpC12Uk9gS9baDl411NQApFmC26EwAOH8WgZl5MYYxFfc+NcPb1dKGj7Sk" crossorigin="anonymous">
<style>
h1{
font-family: 'Pacifico', cursive;
}
body,html {
height: 100%;
}
</style>
<title>Page Counter</title>
</head>
<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>
</h1>
</div>
<script src="https://code.jquery.com/jquery-3.5.1.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
<script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/js/bootstrap.min.js" integrity="sha384-OgVRvuATP1z7JjHLkuOU7Xw704+h835Lr+6QL9UvYjZE3Ipu6Tp75j7Bh/kR0JKI" crossorigin="anonymous"></script>
</body>
</html>
Now you have to create session table in your database for that you have to migrate
python manage.py migrate
Now your project is ready, run it
python manage.py 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.