Django

Display Summary of Marks from Marks List

๐Ÿ“Œ 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 marks
  • len() โ†’ number of subjects
  • max() โ†’ highest marks
  • min() โ†’ 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

  1. View creates list of marks
  2. Performs calculations
  3. Sends data to template
  4. 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

  1. Count number of failed students
  2. Display grade (A/B/C) based on average
  3. Show marks in table format
  4. 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

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 *