To design a website to calculate the bmi of a person in server side.
Clone the repository from GitHub.
Create Django Admin project.
Create a New App under the Django Admin project.
Create python programs for views and urls to perform server side processing.
Create a HTML file to implement form based input and output.
Publish the website in the given URL.
urls.py
from django.contrib import admin
from django.urls import path
from bmiapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.calculate_bmi, name='calculate_bmi'),
]
views.py
from django.shortcuts import render
def calculate_bmi(request):
bmi = None # Default value
if request.method == "POST":
height = float(request.POST.get("height"))
weight = float(request.POST.get("weight"))
bmi = weight / (height * height)
# Print to server console for debugging
print("Height:", height)
print("Weight:", weight)
print("BMI calculated:", bmi)
return render(request, "bmiapp/template.html", {"BMI": bmi})
template.html
<!DOCTYPE html>
<html>
<head>
<title>BMI Calculator</title>
</head>
<body bgcolor="lightblue">
<center>
<h2>BMI Calculator</h2>
<form method="POST">
{% csrf_token %}
<label>Height (m):</label><br>
<input type="text" name="height"><br><br>
<label>Weight (kg):</label><br>
<input type="text" name="weight"><br><br>
<button type="submit">Calculate</button>
</form>
{% if BMI %}
<h3>Your BMI is: {{ BMI }}</h3>
{% endif %}
</center>
</body>
</html>
The program for performing server side processing is completed successfully.

