๐ Introduction
In Django, we can process data in the view and display results in the template.
In this program, we will:
- Use a list of marks
- Calculate total, average, highest, lowest
- Count number of passing students
๐ฏ Program Statement
๐ Given a list of marks, display total, average, highest, lowest, and pass count.
โ๏ธ 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 (Core Logic)
๐ File: views.py
๐น Path:
myproject/myapp/views.py
๐น Code:
from django.shortcuts import render
def marks_analysis(request):
marks = [45, 67, 89, 34, 78, 90, 23]
total = sum(marks)
avg = total / len(marks)
highest = max(marks)
lowest = min(marks)
pass_count = 0
for m in marks:
if m >= 40:
pass_count += 1
data = {
'marks': marks,
'total': total,
'average': avg,
'highest': highest,
'lowest': lowest,
'pass_count': pass_count
}
return render(request, 'marks.html', data)
๐ง Explanation
sum()โ total markslen()โ number of subjectsmax()โ highest marksmin()โ lowest marks- Loop โ count pass students
โ๏ธ 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('marks/', views.marks_analysis),
]
โ๏ธ Step 6: Create Template
๐ File: marks.html
๐น Path:
myproject/templates/marks.html
๐น Code:
<!DOCTYPE html>
<html>
<head>
<title>Marks Analysis</title>
</head>
<body>
<h1>Marks Analysis</h1>
<h3>Marks List:</h3>
<ul>
{% for m in marks %}
<li>{{ m }}</li>
{% endfor %}
</ul>
<hr>
<p>Total = {{ total }}</p>
<p>Average = {{ average }}</p>
<p>Highest = {{ highest }}</p>
<p>Lowest = {{ lowest }}</p>
<p>Pass Count = {{ pass_count }}</p>
</body>
</html>
โ๏ธ Step 7: Run Server
python manage.py runserver
๐ Step 8: Output
Open:
๐ http://127.0.0.1:8000/marks/
โ Output:
Marks: 45, 67, 89, 34, 78, 90, 23
Total = 426
Average = 60.85
Highest = 90
Lowest = 23
Pass Count = 5
๐ง How It Works
- View creates list of marks
- Performs calculations
- Sends data to template
- Template displays results
โ ๏ธ Common Errors
โ Division error
๐ Ensure:
avg = total / len(marks)
โ Loop not working
๐ Check:
{% for m in marks %}
โ Pass count incorrect
๐ Check condition:
if m >= 40
๐งช Practice Questions
- Count number of failed students
- Display grade (A/B/C) based on average
- Show marks in table format
- Take marks from user (advanced)
๐ค Viva Questions & Answers
1. Where should calculations be done in Django?
Calculations should be done in the view because it handles logic. Templates are only for displaying results.
2. What is the use of sum() function?
The sum() function calculates the total of all values in a list. It is used here to find total marks.
3. How is average calculated?
Average is calculated by dividing total marks by number of subjects. It gives overall performance.
4. What is pass count?
Pass count is the number of marks greater than or equal to a threshold (e.g., 40). It indicates how many subjects are cleared.
5. What is max() and min()?
max() returns the highest value in the list, while min() returns the lowest value.
6. Why do we use loop in pass count?
Loop checks each value in list and counts how many satisfy the condition. It is necessary for conditional counting.
7. What is dynamic data?
Dynamic data is generated at runtime based on logic. Marks analysis is dynamic because values can change.
8. Can we calculate in template?
Basic operations are possible but not recommended. Logic should always be handled in the view.
9. What is context dictionary?
It is used to pass multiple values from view to template. Each key becomes a variable in HTML.
10. What happens if list is empty?
Division by zero error may occur. So we must handle empty list carefully.
๐ Next Post: Accept Number as URL Parameter and Display Square
๐ 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
