Django

Create a BMI Calculator using a POST Form

๐Ÿ“Œ Introduction

BMI (Body Mass Index) is used to check whether a person is:

  • Underweight
  • Normal
  • Overweight
  • Obese

(Weight in kg, Height in meters)


๐ŸŽฏ Program Statement

๐Ÿ‘‰ Create a BMI calculator using a POST form.


๐Ÿง  Concept

This program includes:

  • Input: Weight & Height
  • Processing: BMI calculation
  • Output: BMI value + category

โš™๏ธ Step 1: Create Form


๐Ÿ“ File: forms.py

๐Ÿ”น Path:

myproject/myapp/forms.py

๐Ÿ”น Code:

from django import forms

class BMIForm(forms.Form):
weight = forms.FloatField(label='Enter Weight (kg)')
height = forms.FloatField(label='Enter Height (meters)')

โš™๏ธ Step 2: Create View


๐Ÿ“ File: views.py

๐Ÿ”น Path:

myproject/myapp/views.py

๐Ÿ”น Code:

from django.shortcuts import render
from .forms import BMIForm

def bmi_calculator(request):
bmi = None
category = None

if request.method == 'POST':
form = BMIForm(request.POST)
if form.is_valid():
weight = form.cleaned_data['weight']
height = form.cleaned_data['height']

bmi = weight / (height ** 2)

# Category logic
if bmi < 18.5:
category = "Underweight"
elif bmi < 25:
category = "Normal"
elif bmi < 30:
category = "Overweight"
else:
category = "Obese"
else:
form = BMIForm()

return render(request, 'bmi.html', {
'form': form,
'bmi': bmi,
'category': category
})

โš™๏ธ Step 3: URL Mapping


๐Ÿ“ File: urls.py

๐Ÿ”น Path:

myproject/myproject/urls.py

๐Ÿ”น Code:

from django.contrib import admin
from django.urls import path
from myapp import views

urlpatterns = [
path('admin/', admin.site.urls),
path('bmi/', views.bmi_calculator, name='bmi'),
]

โš™๏ธ Step 4: Create Template


๐Ÿ“ File: bmi.html

๐Ÿ”น Path:

myproject/templates/bmi.html

๐Ÿ”น Code:

<!DOCTYPE html>
<html>
<head>
<title>BMI Calculator</title>
</head>
<body>

<h1>BMI Calculator</h1>

<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Calculate BMI</button>
</form>

<hr>

{% if bmi %}
<h2>Your BMI: {{ bmi|floatformat:2 }}</h2>
<h3>Category: {{ category }}</h3>
{% endif %}

</body>
</html>

โš™๏ธ Step 5: Run Server

python manage.py runserver

๐ŸŒ Step 6: Output

๐Ÿ‘‰ http://127.0.0.1:8000/bmi/


โœ… Example Input:

  • Weight: 70 kg
  • Height: 1.75 m

โœ… Output:

Your BMI: 22.86
Category: Normal

๐Ÿง  How It Works

  1. User enters weight & height
  2. Data submitted via POST
  3. Django validates form
  4. BMI calculated in view
  5. Category determined
  6. Result displayed in template

๐Ÿ”ฅ Key Concepts


FloatField

forms.FloatField()

Used for decimal input


Conditional Logic

if bmi < 18.5:

Used to classify BMI


Template Filter

{{ bmi|floatformat:2 }}

Rounds value to 2 decimal places


โš ๏ธ Common Errors


โŒ Division by zero

๐Ÿ‘‰ If height = 0 โ†’ error

๐Ÿ‘‰ Improve:

if height > 0:

โŒ Forgot CSRF token

๐Ÿ‘‰ Form will not submit


โŒ Wrong unit

๐Ÿ‘‰ Ensure:

  • Weight in kg
  • Height in meters

โŒ Result not showing

๐Ÿ‘‰ Use:

{% if bmi %}

๐Ÿงช Practice Questions

  1. Add validation for height > 0
  2. Display BMI category in color
  3. Add input placeholders
  4. Convert cm to meters automatically

๐ŸŽค Viva Questions & Answers



1. Why use FloatField in this program?

Because weight and height may contain decimal values.


2. What is floatformat filter?

It is used to format decimal numbers to a fixed number of decimal places.


3. What happens if height is zero?

Division by zero error will occur, so validation should be added.


4. Why is BMI calculated in view and not template?

Because templates should not contain business logic; calculations belong to views.


5. What is form validation?

It ensures that input data is correct and safe before processing.



๐Ÿ‘‰ Next Post: Marks Form (Total & Average)
๐Ÿ‘‰ Back to List: Django Programs (60 Questions with Solutions)


Further Reading

Introduction to Django Framework and its Features

Django Practice Exercise

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

programmingempire

princites.com


You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *