๐ Introduction
In Django, calculations are usually performed in the view, and the results are sent to the template for display.
In this program, we will:
- Take two fixed numbers
- Calculate sum and product
- Display results using a template
๐ฏ Program Statement
๐ Display the sum and product of two fixed numbers using template rendering.
โ๏ธ Step 1: Create Project and App
django-admin startproject myproject
cd myproject
python manage.py startapp myapp
โ๏ธ Step 2: Register App
๐ File: settings.py
๐น Path:
myproject/myproject/settings.py
๐น Code:
INSTALLED_APPS = [
...
'myapp',
]
โ๏ธ Step 3: Configure Templates
๐ File: settings.py
๐น Path:
myproject/myproject/settings.py
๐น Code:
import os
TEMPLATES = [
{
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
},
]
โ๏ธ Step 4: Create View
๐ File: views.py
๐น Path:
myproject/myapp/views.py
๐น Code:
from django.shortcuts import render
def calculate(request):
a = 10
b = 5
sum_result = a + b
product_result = a * b
data = {
'a': a,
'b': b,
'sum': sum_result,
'product': product_result
}
return render(request, 'calc.html', data)
๐ง Explanation
- Two numbers are defined:
a = 10,b = 5 - Sum and product are calculated in the view
- Values are passed using a dictionary
- Template displays them
โ๏ธ Step 5: 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('calculate/', views.calculate),
]
โ๏ธ Step 6: Create Template
๐ File: calc.html
๐น Path:
myproject/templates/calc.html
๐น Code:
<!DOCTYPE html>
<html>
<head>
<title>Calculation</title>
</head>
<body>
<h1>Sum and Product</h1>
<p>First Number: {{ a }}</p>
<p>Second Number: {{ b }}</p>
<h3>Sum = {{ sum }}</h3>
<h3>Product = {{ product }}</h3>
</body>
</html>
โ๏ธ Step 7: Run Server
python manage.py runserver
๐ Step 8: Output
Open:
๐ http://127.0.0.1:8000/calculate/
โ Output:
First Number: 10
Second Number: 5
Sum = 15
Product = 50
๐ง How It Works
- User visits
/calculate/ - Django calls
calculate()view - View calculates values
- Data sent to template
- Template displays results
โ ๏ธ Common Errors
โ Variables not showing
๐ Ensure names match:
{{ sum }}
{{ product }}
โ Template not found
๐ Check:
templatesfolder exists- Path added in
settings.py
โ Page not loading
๐ Check URL:
/calculate/
๐งช Practice Questions
- Change numbers to 20 and 4
- Also display subtraction and division
- Display result in table format
- Take values from user (advanced)
๐ค Viva Questions & Answers
1. Where should calculations be done in Django?
Calculations should be done in the view, not in the template. Templates are meant only for displaying data.
2. What is template rendering?
Template rendering means combining HTML with dynamic data from the view. Django replaces variables with actual values.
3. Why do we use a dictionary in render()?
A dictionary is used to pass multiple values to the template. Each key becomes a variable accessible in HTML.
4. Can we perform calculations in templates?
Basic operations are possible, but complex logic should be avoided. Best practice is to perform calculations in the view.
5. What is the role of render() here?
The render() function sends calculated values to the HTML file and returns the final response to the browser.
6. What happens if variable name is incorrect?
If variable names do not match, the template will not display the values. It may show blank output.
7. What is dynamic content?
Dynamic content changes based on data or logic. In this program, sum and product are dynamically generated.
8. Why separate logic and presentation?
To maintain clean code structure. Logic stays in views, and UI stays in templates.
9. What is the use of {{ }} in templates?
Double curly braces are used to display variables. Django replaces them with actual values.
10. Can we pass multiple values to template?
Yes, multiple values can be passed using a dictionary. Each value can be accessed using its key.
๐ Navigation
๐ Next Post: Display List Using {% for %} Loop in Template
๐ 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
