3.
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
from django.http import HttpResponse
from django.shortcuts import render
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
1. Create a Django project: django-admin startproject mywebsite
2. Enter into Django project mywesite : cd mywebsite
3. Create a Django app: python manage.py startapp main
4. Add the app to the project: Open mywebsite/settings.py and add 'main' to INSTALLED_APPS:
5. Create a templates directory: Create a templates directory inside the main app folder
6. Create layout.html , about.html, home.html and contact.html files in templates folder as
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 title %}About Us - My Website{% endblock %}
{% block content %}
<h1>About Us</h1>
<p>This is the About Us page.</p>
{% endblock %}
Contact.html
{% extends 'layout.html' %}
{% block title %}Contact Us - My Website{% endblock %}
{% block content %}
<h1>Contact Us</h1>
<p>This is the Contact Us page.</p>
{% endblock %}
home.html
{% extends 'layout.html' %}
{% block title %}Home - My Website{% endblock %}
{% block content %}
<h1>Welcome to My Website</h1>
<p>This is the home page.</p>
{% endblock %}
7. Create URLs in the app: Open main/urls.py and add the following content:
1. from django.urls import path
2. from . import views
3.
4. urlpatterns = [
5.
6. path('mainpage/', views.mainpage, name='mainpage'),
7. path('home/', views.home, name='home'),
8. path('about/', views.about, name='about'),
9. path('contact/', views.contact, name='contact'),
10. ]
11.
8. Include app URLs in the project: Open mywebsite/urls.py and modify it to include the app's URLs:
from django.contrib import admin
from django.urls import path, include
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