0% found this document useful (0 votes)
3 views7 pages

Create A New Django Project Named Calculator

The document provides step-by-step instructions for creating a Django project named 'calculator_project' and a Flask application for grade calculation. It details the setup of a Django app for arithmetic operations, including URL configuration, view functions, and HTML templates, as well as a Flask app for calculating grades based on user input. Both applications include instructions for running the server and accessing the web interfaces in a browser.

Uploaded by

Lakshmi Marineni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views7 pages

Create A New Django Project Named Calculator

The document provides step-by-step instructions for creating a Django project named 'calculator_project' and a Flask application for grade calculation. It details the setup of a Django app for arithmetic operations, including URL configuration, view functions, and HTML templates, as well as a Flask app for calculating grades based on user input. Both applications include instructions for running the server and accessing the web interfaces in a browser.

Uploaded by

Lakshmi Marineni
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Create a new Django project named calculator_project:

django-admin startproject calculator_project

cd calculator_project

Create a new Django app named calculator:

python manage.py startapp calculator

Configure settings.py

INSTALLED_APPS = [

'django.contrib.admin',

'django.contrib.auth',

'django.contrib.contenttypes',

'django.contrib.sessions',

'django.contrib.messages',

'django.contrib.staticfiles',

'calculator', # Add the calculator app here

Step 4: Define URLs


Open calculator_project/urls.py and update it:

from django.contrib import admin

from django.urls import path

from calculator import views

urlpatterns = [

path('admin/', admin.site.urls),

path('', views.calculator, name='calculator'),

]
Step 5: Create the View Function
Edit calculator/views.py:

from django.shortcuts import render

def calculator(request):

result = None

if request.method == "POST":

num1 = float(request.POST.get("num1"))

num2 = float(request.POST.get("num2"))

operation = request.POST.get("operation")

if operation == "add":

result = num1 + num2

elif operation == "subtract":

result = num1 - num2

elif operation == "multiply":

result = num1 * num2

elif operation == "divide":

result = num1 / num2 if num2 != 0 else "Cannot divide by zero"

return render(request, "calculator.html", {"result": result})

Step 6: Create an HTML Template

Inside the calculator app, create a templates folder and a file named calculator.html:

calculator/

│── templates/

│ ├── calculator.html
Now, create calculator/templates/calculator.html and add:

<!DOCTYPE html>

<html>

<head>

<title>Simple Django Calculator</title>

</head>

<body>

<h2>Arithmetic Calculator</h2>

<form method="post">

{% csrf_token %}

<label>Number 1:</label>

<input type="text" name="num1" required><br><br>

<label>Number 2:</label>

<input type="text" name="num2" required><br><br>

<label>Operation:</label>

<select name="operation">

<option value="add">Addition (+)</option>

<option value="subtract">Subtraction (-)</option>

<option value="multiply">Multiplication (*)</option>

<option value="divide">Division (/)</option>

</select><br><br>

<button type="submit">Calculate</button>

</form>

{% if result is not None %}

<h3>Result: {{ result }}</h3>

{% endif %}

</body>
</html>

Step 7: Run Migrations

python manage.py migrate

Step 8: Run the Server

python manage.py runserver

Open http://127.0.0.1:8000/ in your browser.

Flask Grade Calculator Application


Let's create a simple Flask application where users can enter their marks, and it will calculate the
grade (A, B, C, D, F) based on the input score.

Step 1: Install Flask

pip install flask

Step 2: Create a Flask App


Create a new file app.py and add the following code:

from flask import Flask, render_template, request

app = Flask(__name__)

def calculate_grade(average_marks):

if average_marks >= 90:

return "A"

elif average_marks >= 80:

return "B"

elif average_marks >= 70:

return "C"
elif average_marks >= 60:

return "D"

else:

return "F"

@app.route('/', methods=['GET', 'POST'])

def grade_calculator():

grade = None

average_marks = None

if request.method == 'POST':

try:

# Get marks from form

marks = [

float(request.form['subject1']),

float(request.form['subject2']),

float(request.form['subject3']),

float(request.form['subject4']),

float(request.form['subject5'])

# Calculate average

average_marks = sum(marks) / len(marks)

grade = calculate_grade(average_marks)

except ValueError:

grade = "Invalid Input! Please enter valid numbers."

return render_template('index.html', grade=grade, average_marks=average_marks)

if __name__ == '__main__':

app.run(debug=True)
Step 3: Create HTML Template
Inside your Flask project, create a templates/ folder and inside it, create index.html.

grade_calculator/

│── app.py

│── templates/

│ ├── index.html

Now, create templates/index.html:

<!DOCTYPE html>

<html>

<head>

<title>Grade Calculator</title>

</head>

<body>

<h2>Enter Your Marks for 5 Subjects</h2>

<form method="post">

<label>Subject 1:</label>

<input type="number" name="subject1" required><br><br>

<label>Subject 2:</label>

<input type="number" name="subject2" required><br><br>

<label>Subject 3:</label>

<input type="number" name="subject3" required><br><br>

<label>Subject 4:</label>

<input type="number" name="subject4" required><br><br>

<label>Subject 5:</label>
<input type="number" name="subject5" required><br><br>

<button type="submit">Calculate Grade</button>

</form>

{% if grade %}

<h3>Average Marks: {{ average_marks }}</h3>

<h3>Your Grade: {{ grade }}</h3>

{% endif %}

</body>

</html>

Step 4: Run the Flask App


Save your files and start the Flask server:

python app.py

This will start the server at http://127.0.0.1:5000/. Open the link in a browser.

You might also like