Django

Display Sum and Product of Two Numbers Using Template Rendering

๐Ÿ“Œ 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

  1. User visits /calculate/
  2. Django calls calculate() view
  3. View calculates values
  4. Data sent to template
  5. Template displays results

โš ๏ธ Common Errors


โŒ Variables not showing

๐Ÿ‘‰ Ensure names match:

{{ sum }}
{{ product }}

โŒ Template not found

๐Ÿ‘‰ Check:

  • templates folder exists
  • Path added in settings.py

โŒ Page not loading

๐Ÿ‘‰ Check URL:

/calculate/

๐Ÿงช Practice Questions

  1. Change numbers to 20 and 4
  2. Also display subtraction and division
  3. Display result in table format
  4. 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

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 *