π Introduction
Temperature conversion is a common programming exercise.
In this program, we will:
- take temperature in Celsius
- convert it into Fahrenheit
- display the result on the same page
The conversion formula is:

This program is useful for understanding:
- Django forms
- POST method
- arithmetic logic in views
π― Program Statement
π Create a temperature converter form (Celsius to Fahrenheit).
π§ Concept
This program uses:
forms.pyfor input fieldviews.pyfor conversion formulatemplatefor displaying form and result
βοΈ Step 1: Create Form
π File: forms.py
πΉ Path:
myproject/myapp/forms.py
πΉ Code:
from django import forms
class TemperatureForm(forms.Form):
celsius = forms.FloatField(label='Enter Temperature in Celsius')
π§ Explanation
- We created one field named
celsius FloatFieldis used so the program can accept decimal temperatures also
βοΈ Step 2: Create View
π File: views.py
πΉ Path:
myproject/myapp/views.py
πΉ Code:
from django.shortcuts import render
from .forms import TemperatureForm
def temperature_converter(request):
fahrenheit = None
celsius_value = None
if request.method == 'POST':
form = TemperatureForm(request.POST)
if form.is_valid():
celsius_value = form.cleaned_data['celsius']
fahrenheit = (9 / 5) * celsius_value + 32
else:
form = TemperatureForm()
return render(request, 'temperature.html', {
'form': form,
'celsius': celsius_value,
'fahrenheit': fahrenheit
})
π§ Explanation
- Input is collected from form
- Formula is applied in the view
- Both Celsius and Fahrenheit values are passed to the template
βοΈ 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('temperature/', views.temperature_converter, name='temperature_converter'),
]
βοΈ Step 4: Create Template
π File: temperature.html
πΉ Path:
myproject/templates/temperature.html
πΉ Code:
<!DOCTYPE html>
<html>
<head>
<title>Temperature Converter</title>
</head>
<body>
<h1>Celsius to Fahrenheit Converter</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Convert</button>
</form>
<hr>
{% if fahrenheit is not None %}
<h2>Celsius: {{ celsius }}</h2>
<h2>Fahrenheit: {{ fahrenheit|floatformat:2 }}</h2>
{% endif %}
</body>
</html>
π§ Explanation
- Form is displayed using
{{ form.as_p }} - The result is shown only after submission
floatformat:2shows the Fahrenheit value up to 2 decimal places
βοΈ Step 5: Run Server
python manage.py runserver
π Step 6: Output
Open:
π http://127.0.0.1:8000/temperature/
β Example Input:
- Celsius = 37
β Output:
Celsius: 37
Fahrenheit: 98.60
π§ How It Works
- User opens the temperature form page
- Enters Celsius value
- Form is submitted using POST
- Django validates the value
- View applies conversion formula
- Template displays the result
π₯ Key Concepts
Django Form
class TemperatureForm(forms.Form):
Defines the input structure.
FloatField
celsius = forms.FloatField(...)
Allows decimal values.
Conversion Formula
fahrenheit = (9 / 5) * celsius_value + 32
Converts Celsius to Fahrenheit.
Conditional Display
{% if fahrenheit is not None %}
Shows output only after form submission.
β οΈ Common Errors
β Forgot {% csrf_token %}
POST form will fail due to CSRF verification error.
β Wrong formula
Use:
(9 / 5) * celsius + 32
and not 9 / (5 * celsius) + 32.
β Using CharField instead of FloatField
Temperature should be numeric, so FloatField is appropriate.
β Output not showing for 0Β°C
Use:
{% if fahrenheit is not None %}
instead of {% if fahrenheit %} because 0 can cause problems in some checks.
β Import error in views.py
Import form correctly:
from .forms import TemperatureForm
π§ͺ Practice Questions
- Convert Fahrenheit to Celsius
- Create a two-way converter
- Add Kelvin conversion
- Display a message like βBoiling point of waterβ when Celsius = 100
π€ Viva Questions & Answers
1. Why is FloatField used in this program?
FloatField is used because temperature values can be decimals such as 36.5 or 98.6.
2. Why is POST method used here?
POST method is used because the user submits input data to the server for processing. It is safer than GET for forms.
3. What is the role of forms.py?
forms.py defines the form fields and their types. It makes input handling cleaner and more structured.
4. What does is_valid() do in this program?
It checks whether the submitted Celsius value is valid before applying the conversion formula.
5. Why is the conversion logic written in the view?
The view handles program logic and calculations, while the template only displays output. This keeps code organized.
6. What is floatformat:2 in the template?
It is a Django template filter used to display the Fahrenheit result up to 2 decimal places.
7. Can this program accept negative temperatures?
Yes, FloatField allows negative values, so temperatures below 0Β°C can also be converted.
8. Why do we use {% csrf_token %} in the form?
It is required for security in Django POST forms and protects against CSRF attacks.
π Next Post: Create a Django Form to Determine Whether a Number is Even or Odd
π Back to List: Django Programs (60 Questions with Solutions)
Further Reading
Introduction to Django Framework and its Features
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
