Program 3 & 4
Program 3 & 4
Develop a simple Django app that displays an unordered list of fruits and
ordered list of selected students for an event
Index.html
<html>
</head>
<title>Home Page</title>
</head>
<body>
<h1>List of Fruits</h1>
<ul>
{% for item in fruit_list %}
<li>{{item}}</li>
{%endfor%}
</ul>
<h1>List of students</h1>
<ul>
{% for student in student_list %}
{%if student.score > 75%}
<li>{{student.name}}</li>
{%endif%}
{%endfor%}
</ul>
</body>
</html>
View.py
def homepage(request):
fruits=['Apple','Banana','Orange','Pineapple']
students = [
{'name': 'Harish', 'score': 85},
{'name': 'Briana', 'score': 70},
{'name': 'John', 'score': 95},
{'name': 'Arul', 'score': 60},
]
context={'fruit_list':fruits,'student_list':students,}
return render(request,"index.html",context)
ulrs.py
from django.contrib import admin
from django.urls import path
from .views import homepage
urlpatterns = [
path('admin/', admin.site.urls),
path('home/',homepage),
]
Output
4. Develop a layout.html with a suitable header (containing navigation menu) and
footer with copyright and developer information. Inherit this layout.html and create
3 additional pages: contact us, About Us and Home page of any website
5. Create a templates directory: Create a templates directory inside the main app folder
layout.html
1. <!DOCTYPE html>
2. <html lang="en">
3. <head>
4. <title>{% block title %}My Website{% endblock %}</title>
5.
6. </head>
7. <body>
8.
9.
10. <ul>
11. <li><a href="{% url 'mainpage' %}">layout</a></li>
12. <li><a href="{% url 'home' %}">Home</a></li>
13. <li><a href="{% url 'about' %}">About Us</a></li>
14. <li><a href="{% url 'contact' %}">Contact Us</a></li>
15.
16. </ul>
17.
18.
19. {% block content %}
20.
21. <h1> Welcome to Main Page </h1>
22.
23. {% endblock %}
24.
25. <p> Welcome to RLJIT Doddaballpur .</p>
26.
27. </body>
28. </html>
29.
About.html
{% extends 'layout.html' %}
{% block content %}
<h1>About Us</h1>
{% endblock %}
Contact.html
{% extends 'layout.html' %}
{% block content %}
<h1>Contact Us</h1>
home.html
{% extends 'layout.html' %}
{% block content %}
<h1>Welcome to My Website</h1>
{% endblock %}
7. Create URLs in the app: Open main/urls.py and add the following content:
8. Include app URLs in the project: Open mywebsite/urls.py and modify it to include the app's URLs:
urlpatterns = [
path('admin/', admin.site.urls),
path('', include('main.urls')),
]
9. Define views: Open main/views.py and define the views for the pages
1. from django.shortcuts import render
2.
3. def mainpage(request):
4. return render(request, 'layout.html')
5.
6. def home(request):
7. return render(request, 'home.html')
8.
9. def about(request):
10. return render(request, 'about.html')
11.
12. def contact(request):
13. return render(request, 'contact.html')
Output:
Tree-structure for Reference