0% found this document useful (0 votes)
36 views11 pages

FSD Lab Component-2

DJANGO STUDENT REGISTRATION FILE

Uploaded by

sunil.ise
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)
36 views11 pages

FSD Lab Component-2

DJANGO STUDENT REGISTRATION FILE

Uploaded by

sunil.ise
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

SUBJECT: FULLSTACK DEVELOPMENT (21CS62)

LAB COMPONENT SOLUTIONS

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.

[Link]
from datetime import date
from [Link] import HttpResponse from
[Link] import render
from [Link] 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)

[Link]
from [Link] import admin
from [Link] import path, re_path
from [Link] import create_table_of_squares
urlpatterns = [
path('admin/', [Link]), 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.

[Link]
def vc(request,sentence):
vow_cnt=0
cons_cnt=0 vow_dict=dict()
cons_dict=dict()
for letter in sentence: if
[Link]():
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

result="<h1>%d Vowels and %d Consonants</h1>" % (vow_cnt,cons_cnt)


result+="<h2>Vowel Counter</h2>"
for key,value in vow_dict.items():
result+="<p>%s:%d</p>"%(key,value)
result+="<h2>Consonant Counter</h2>" for
key,value in cons_dict.items():
result+="<p>%s:%d</p>"%(key,value) return

HttpResponse(result)

[Link]
from [Link] import admin
from [Link] import path, re_path
from [Link] import create_table_of_squares,vc
urlpatterns = [

path('cts/<int:s>/<int:n>', create_table_of_squares), path('vc/<str:sentence>', vc),

Output:

2|Pag
e
Develop a Django app that finds the mode of a given set of numbers specified in
the URL

[Link]
def find_mode(request,listofnum):
arr=[Link](",") 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)

[Link]
from [Link] import admin
from [Link] import path, re_path
from [Link] import create_table_of_squares,vc,find_mode
urlpatterns = [
path('admin/', [Link]), 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

[Link]
from datetime import date
from [Link] import HttpResponse from
[Link] import render
from [Link] import Context, Template

# Create your views here.


def showlist(request):
fruits=["Mango","Apple","Bananan","Jackfruits"]
student_names=["Tony","Mony","Sony","Bob"]
return render(request, '[Link]', {"fruits":fruits,"student_names":student_names}
)

[Link]

from [Link] import admin


from [Link] import path, re_path
from [Link] import showlist

urlpatterns = [
path('admin/', [Link]),
path('showlist/', showlist),

5|Pag
e
Template HTML file (inside ap2/templates subfolder)
[Link]
<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

[Link]
from datetime import date
from [Link] import HttpResponse from
[Link] import render
from [Link] 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})

[Link]
from [Link] import admin
from [Link] import path, re_path
from [Link] import create_table_of_squares,vc,find_mode from
[Link] import list_of_subjects

urlpatterns = [
path('admin/', [Link]),
path('list_of_subjects/', list_of_subjects),
]

Template file: list_of_subjects.html

<html>
<body>
<table border>
<tr>
<th>Subject Code</th>
<th>Subject Name</th>
</tr>
{% for subject in l %}
{% if [Link]|divisibleby:"2" %}
<tr>

7|Pag
e
<td style="background-color: lightgreen;">{{ [Link] }}</td>
<td style="background-color: lightgreen;">{{ [Link]|upper
}} </td>
</tr>
{% else %}
<tr>
<td>{{ [Link] }}</td>
<td>{{ [Link]|upper }}</td>
</tr>
{% endif %}
{% endfor %}
</table>
</body>

Output:

8|Pag
e
DEPT. OF AIML, JNNCE,
SHIVAMOGGA

2. Develop a [Link] with a suitable header (containing navigation menu) and footer
with copyright and developer information. Inherit this [Link] and create 3
additional pages: contact us, About Us and Home page of any website.

[Link]
from datetime import date
from [Link] import HttpResponse from
[Link] import render
from [Link] import Context, Template
def home(request):
return render(request,'[Link]')

def aboutus(request):
return render(request,'[Link]')

def contactus(request):
return render(request,'[Link]')

[Link]
from [Link] import admin
from [Link] import path, re_path
from [Link] import aboutus, home, contactus

urlpatterns = [
path('admin/', [Link]),
path('aboutus/', aboutus),
path('home/', home),
path('contactus/', contactus),

10 | P a g
e
DEPT. OF AIML, JNNCE,
SHIVAMOGGA
Template files:
[Link]
<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>
&copy; ISE, Developed by SK, Inc.
</footer>
</body>
</ht>

[Link]
{% extends '[Link]' %}
{% block title %}
Home
{% endblock %}
{% block content %}
<h2>This is the home page</h2>
{% endblock %}

[Link]
{% extends '[Link]' %}
{% block title %}
About Us
{% endblock %}
{% block content %}
<h2>We are DJango developers</h2>
{% endblock %}

10 | P a g
e
[Link]
{% extends '[Link]' %}
{% block title %}
Contact us
{% endblock %}
{% block content %}
<h2>Out phone: 9900923050 <br> Address:
K R Puram, Bangalore</h2>
{% endblock %}

Output:

111 | P a
ge

You might also like