
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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, where clicking a button will add a number and it will keep doing that even if you close the tab and keep the data in session. We will get the idea of using sessions for making a simple app and how to transfer data using sessions
Example
In urls.py, add the following lines −
from django.urls import path from django.urls.resolvers import URLPattern from .import views urlpatterns = [ path('', views.counterView, name='counter'), ]
Here we set up views on home url.
In views.py, add the following lines −
from django.shortcuts import render # Create your views here. def counterView(request): if request.method == "POST" and 'count' in request.POST: try: request.session['count'] +=1 except: request.session['count'] = 0 elif request.method == 'POST' and 'reset' in request.POST: request.session['count'] = 0 return render(request,'counter.html')
Here we created a POST request handler, we will send a number from frontend and save it in session's count variable. When reset is sent from the frontend, then the session count becomes 0
Now create a templates folder in app directory and create a counter.html in it and write this −
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=devicewidth, 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" integrity="sha38 4- +0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyT G2x" crossorigin="anonymous"> </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="margintop: 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 textwhite shadow-lg">Count</button> </form> <br> <br> <form method="post"> {% csrf_token %} <button name="reset" class="reset px-5 py-3 textwhite shadow-lg">Reset</button> </form> </div> </body> </html>
Here is frontend which we are rendering on home url.
Output
Clicking the Count button will add 1 in the number and clicking the Reset button will reset the counter to 0.
- Related Articles
- Making a URL shortner app in Django
- Django – Making a Django website more human-like using Humanizer
- How to create a simple counter Using ReactJS?
- Making a Pickle field in Django models
- How to Create an App in Django?
- How to read a simple text file in an Android App using Kotlin?
- Making your own custom filter tags in Django
- Making Two Screen App in Snack
- A simple News app with Tkinter and Newsapi
- How to read a simple text file in Android App?
- Form validation using Django
- Decimal counter using logic controller
- Django – Making a contact form and storing its data without model, query and html
- Counter Size and Counter Overflow
- Adding a DeleteView in Django
