0% found this document useful (0 votes)
5 views

Assignment Day 2 Q2

The document outlines the development of a Django app that displays a list of registered students with their names and ages. It includes a view function that defines student objects and a template that filters and shows only registered students, indicating whether they are majors based on their age. The URL configuration is also provided to route requests to the appropriate view.

Uploaded by

Shweta Nirmanik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Assignment Day 2 Q2

The document outlines the development of a Django app that displays a list of registered students with their names and ages. It includes a view function that defines student objects and a template that filters and shows only registered students, indicating whether they are majors based on their age. The URL configuration is also provided to route requests to the appropriate view.

Uploaded by

Shweta Nirmanik
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

2.

Develop a Django app that send list of student object which has fields
name, age, registeredOrNot (True or False) to template. In the template
only display name and age of students who are registered
(registeredOrNot=True) along with whether they are major or not. (For major
consider age>=18).

Views.py
def student_list(request):
# Define student objects with name, age, and registration status
students = [
{'name': 'Tony', 'age': 35, 'registeredOrNot': True},
{'name': 'Pepper', 'age': 32, 'registeredOrNot': True},
{'name': 'Scarlet', 'age': 28, 'registeredOrNot': True},
{'name': 'Vision', 'age': 3, 'registeredOrNot': False},
{'name': 'Stan', 'age': 90, 'registeredOrNot': True},
{'name': 'Baby Groot', 'age': 5, 'registeredOrNot': False},
{'name': 'Rocket', 'age': 15, 'registeredOrNot': False},
]
return render(request, 'student_list.html', {'students': students})

student_list.html
{% load static %}
<html>
<head>
<meta charset="UTF-8">
<title>Registered Students</title>
</head>
<body>
<h1>Registered Students</h1>
<ul>
{% for student in students %}
{% if student.registeredOrNot %}
<li>
Name: {{ student.name }} <br>
Age: {{ student.age }} <br>
{% if student.age >= 18 %}
Major: Yes
{% else %}
Major: No
{% endif %}
</li>
{% endif %}
{% endfor %}
</ul>
</body>
</html>

Urls.py
from assign2.views import state_list, student_list

urlpatterns = [
path('admin/', admin.site.urls),
path('states/', state_list, name='state_list'),
path('students/', student_list, name='student_list'),

Output

You might also like