To design a website to calculate the power of a lamp filament in an incandescent bulb in the server side.
P = I2R
P --> Power (in watts)
I --> Intensity
R --> Resistance
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.
html
<!DOCTYPE html>
<html>
<head>
<title>BMI Calculator</title>
</head>
<body>
<form method="POST">
{% csrf_token %}
<label>Height (cm):</label>
<input type="text" name="height"><br>
<label>Weight (kg):</label>
<input type="text" name="weight"><br>
<button type="submit">Calculate</button>
</form>
{% if BMI %}
<h3>Your BMI is: {{ BMI }}</h3>
{% endif %}
</body>
</html>
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.calculator, name='calculator'),
]
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('mathapp.urls')),
]
views.py
from django.shortcuts import render
def calculator(request):
power = None
if request.method == 'POST':
try:
intensity = float(request.POST.get('intensity'))
resistance = float(request.POST.get('resistance'))
power = round(intensity ** 2 * resistance, 2)
except (TypeError, ValueError):
power = "Invalid input. Please enter numeric values."
return render(request, 'math.html', {'power': power})
The program for performing server side processing is completed successfully.