FSD Lab Component-2
FSD Lab Component-2
FSD Lab Component-2
Module-1: Additional Programs on Django Views and URLS Develop a Django app
that displays tables of squares of pairs of numbers input in the URL.
Views.py
from datetime import date
from django.http import HttpResponse from
django.shortcuts import render
from django.template import Context, Template def
create_table_of_squares(request,s,n):
result=""
for i in range(1,n+1):
result+="<p>"+str(s)+"*"+str(i)+"="+str((s*i))+"</p>"
return HttpResponse(result)
URLS.py
from django.contrib import admin
from django.urls import path, re_path
from ap2.views import create_table_of_squares
urlpatterns = [
path('admin/', admin.site.urls), path('cts/<int:s>/<int:n>',
create_table_of_squares),
Output
1|Pag
e
Develop a Django app that displays number of vowels and consonants and also list of
vowels and consonants for any input sentence specified in the URL.
Views.py
def vc(request,sentence):
vow_cnt=0
cons_cnt=0 vow_dict=dict()
cons_dict=dict()
for letter in sentence: if
letter.isalpha():
if letter in "aeiouAEIOU": vow_cnt=vow_cnt+1
vow_dict[letter]=vow_dict.get(letter,0)+1 else:
cons_cnt=cons_cnt+1
cons_dict[letter]=cons_dict.get(letter,0)+1
HttpResponse(result)
URLS.py
from django.contrib import admin
from django.urls import path, re_path
from ap2.views import create_table_of_squares,vc
urlpatterns = [
Output:
2|Pag
e
Develop a Django app that finds the mode of a given set of numbers specified in
the URL
Views.py
def find_mode(request,listofnum):
arr=listofnum.split(",") num_count=dict()
for num in arr:
num_count[num]=num_count.get(num,0)+1
num_count=sorted(num_count.items(),key=lambda item:item[1])
num_count.reverse()
result="<p><span style=color:red>%s</span> appears <span style=background-
color:yellow>%s</span> times"%(num_count[0][0],num_count[0][1])
return HttpResponse(result)
URLS.py
from django.contrib import admin
from django.urls import path, re_path
from ap2.views import create_table_of_squares,vc,find_mode
urlpatterns = [
path('admin/', admin.site.urls), path('cts/<int:s>/<int:n>',
create_table_of_squares), path('vc/<str:sentence>', vc),
path('find_mode/<str:listofnum>', find_mode),
3|Pag
e
Output:
4|Pag
e
Module-2: Django Templates and Models
Laboratory Component:
1. Develop a simple Django app that displays an unordered list of fruits and ordered list
of selected students for an event
Views.py
from datetime import date
from django.http import HttpResponse from
django.shortcuts import render
from django.template import Context, Template
URLS.py
urlpatterns = [
path('admin/', admin.site.urls),
path('showlist/', showlist),
5|Pag
e
Template HTML file (inside ap2/templates subfolder)
showlist.html
<html>
<style type="text/css">
#i1 {background-color: lightgreen;color:brown;display:table} #i2
{background-color: black;color:yellow}
</style>
<body>
<h1 id="i1">Unordered list of fruits</h1>
<ul>
{% for fruit in fruits %}
<li>{{ fruit }}</li>
{% endfor %}
</ul>
<h1 id="i2">Ordered list of Students</h1>
<ol>
{% for student in student_names %}
<li>{{ student }}</li>
{% endfor %}
</ol>
</body>
</html>
Output:
6|Pag
e
Develop a Django app that displays list of subject codes and subject names of any
semester in tabular format. Even rows should have a light green background color and
subject names should be in all caps
Views.py
from datetime import date
from django.http import HttpResponse from
django.shortcuts import render
from django.template import Context, Template
def list_of_subjects(request):
s1={"scode":"21CS51","sname":"cn"}
s2={"scode":"21CS52","sname":"ATc"}
s3={"scode":"21CS53","sname":"DbMS"}
s4={"scode":"21AI54","sname":"PAI"}
l=list()
l=[s1,s2,s3,s4]
return render(request,'list_of_subjects.html',{"l":l})
URLS.py
from django.contrib import admin
from django.urls import path, re_path
from ap2.views import create_table_of_squares,vc,find_mode from
ap2.views import list_of_subjects
urlpatterns = [
path('admin/', admin.site.urls),
path('list_of_subjects/', list_of_subjects),
]
<html>
<body>
<table border>
<tr>
<th>Subject Code</th>
<th>Subject Name</th>
</tr>
{% for subject in l %}
{% if forloop.counter|divisibleby:"2" %}
<tr>
7|Pag
e
<td style="background-color: lightgreen;">{{ subject.scode }}</td>
<td style="background-color: lightgreen;">{{ subject.sname|upper
}} </td>
</tr>
{% else %}
<tr>
<td>{{ subject.scode }}</td>
<td>{{ subject.sname|upper }}</td>
</tr>
{% endif %}
{% endfor %}
</table>
</body>
Output:
8|Pag
e
DEPT. OF AIML, JNNCE,
SHIVAMOGGA
2. 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.
Views.py
from datetime import date
from django.http import HttpResponse from
django.shortcuts import render
from django.template import Context, Template
def home(request):
return render(request,'home.html')
def aboutus(request):
return render(request,'aboutus.html')
def contactus(request):
return render(request,'contactus.html')
URLS.py
from django.contrib import admin
from django.urls import path, re_path
from ap2.views import aboutus, home, contactus
urlpatterns = [
path('admin/', admin.site.urls),
path('aboutus/', aboutus),
path('home/', home),
path('contactus/', contactus),
10 | P a g
e
DEPT. OF AIML, JNNCE,
SHIVAMOGGA
Template files:
layout.html
<html>
<title>{% block title %} {% endblock %} </title>
<style type="text/css">
nav {background-color: lightblue;padding:10px}
</style>
<body>
<nav>
<a href="/home/">Home</a>|
<a href="/aboutus/">About Us</a>|
<a href="/contactus/">Contact Us</a>|
</nav>
<section>
{% block content %}{% endblock %}
</section>
<footer>
<hr>
© ISE, Developed by SK, Inc.
</footer>
</body>
</ht>
home.html
{% extends 'layout.html' %}
{% block title %}
Home
{% endblock %}
{% block content %}
<h2>This is the home page</h2>
{% endblock %}
aboutus.html
{% extends 'layout.html' %}
{% block title %}
About Us
{% endblock %}
{% block content %}
<h2>We are DJango developers</h2>
{% endblock %}
10 | P a g
e
contactus.html
{% extends 'layout.html' %}
{% block title %}
Contact us
{% endblock %}
{% block content %}
<h2>Out phone: 9900923050 <br> Address:
K R Puram, Bangalore</h2>
{% endblock %}
Output:
111 | P a
ge