Projects Python

Calculator using Django Python

Build a feature-rich calculator using Django Python, For online mathematical Operation in Few Steps. You Can Download Source code of Calculator Using Django Python.


Setup your Django Project

Assuming you have Already Django installed, let’s create a new Django project and app. You Have to Open your terminal and run these commands

  • Create a new Django project 
django-admin startproject calculator_project
  • Navigate to the project directory
cd calculator_project
  • Create a new Django app within the project
python startapp calculator_app


Design the Calculator Interface

Navigate to the calculator_app directory and open the file. Define a view that will handle the calculator interface

Python Code

from django.shortcuts import render
from django.views import View

class CalculatorView(View):
template_name = ‘calculator.html’

def get(self, request):
return render(request, self.template_name)

Step 3 

Create the Calculator Template

Inside the calculator_app directory, create a folder named templates. Inside the templates folder, create another folder named calculator_app. Then, create a file named calculator.html inside the calculator_app folder.

Your directory structure should look like this 


In the calculator.html file, you’ll create the HTML structure for the calculator interface


<!DOCTYPE html>
<form method=”post”>
{% csrf_token %}
<input type=”text” name=”expression” id=”expression” placeholder=”Enter an expression”>
<button type=”submit”>Calculate</button>
<h2>Result: {{ result }}</h2>

Step 4

Implement the Calculation Logic.

In the file, modify the CalculatorView class to handle the calculation logic and render the result

Python Code 

from django.shortcuts import render
from django.views import View
from django.http import HttpResponse

class CalculatorView(View):
template_name = ‘calculator_app/calculator.html’

def get(self, request):
return render(request, self.template_name, {‘result’: None})

def post(self, request):
expression = request.POST.get(‘expression’)
if expression:
result = eval(expression)
return render(request, self.template_name, {‘result’: result})
except Exception as e:
return render(request, self.template_name, {‘result’: ‘Error: Invalid expression’})
return render(request, self.template_name, {‘result’: None})

Step 5

Define URLs

Open the file in the calculator_app directory and define the URL route for the CalculatorView.

Python Code

from django.urls import path
from .views import CalculatorView

urlpatterns = [
path(”, CalculatorView.as_view(), name=’calculator’),

Step 6 

Add the App URL to the Project.

In the file of the calculator_project directory, include the app’s URLs


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

urlpatterns = [
path(‘calculator/’, include(‘calculator_app.urls’)),

Step 7

Run the Development Server

In the terminal, navigate to the project directory (calculator_project) and run the development server


python runserver

Access the calculator by visiting in your web browser. You should see the calculator interface where you can enter expressions and see the calculated results.

Remember that this example uses the eval function to evaluate expressions, which can be a security risk if you’re allowing user-generated input. In a real-world scenario, you should consider using a safer expression evaluation method.

This is a basic example of creating a calculator using Django. You can enhance it by adding more features like handling different operators, validating input, and improving the user interface


Download Source code 

Leave a Comment