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

FD Lab Component 2

DJANGO LIST DISPLAY PROGRAM

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)
44 views

FD Lab Component 2

DJANGO LIST DISPLAY PROGRAM

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
You are on page 1/ 15

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.

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

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)

URLS.py
from django.contrib import admin
from django.urls import path, re_path
from ap2.views 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

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),

Output:
3|Pag
e
4|Pag
e
Module-2: Django Templates and Models

Template example program:


Views.py
from datetime import date
from django.http import HttpResponse from
django.shortcuts import render
from django.template import Context, Template

def template_test(request): t=Template("""


<html>
<body>
{% if attending %}
<h1>Welcome {{ participant.name|upper }},{{
participant.dept}} to FDP {{ fdp_name }}
on {{ fdpdate|date:"F j,Y"}} </h1>
{% if atd_per > 80 %}
<h2> Very Good </h2>
{% elif atd_per > 60 %}
<h2> Good </h2>
{% else %}
<h2> Not satisfactory </h2>
{% endif %}
Your phone no is {{ participant.pno}}
<h1>List of Topics</h1>
<ul>
{% for topic in topics %}
<li>{{ forloop.revcounter0}}: {{ topic }}</li>
{% endfor %}
{% else %}
<h1>Thank you </h1>
{% endif %}

</body>
</html>

5|Pag
e
""")
fdp_name="Programming with Julia"
atd_per=66
participant={"name":"Chetan","pno":"9900923050","dept":"AIML"}
topics=["Models","Views","Templates","AJAX","NonHTML"]
c=Context({"fdp_name":
fdp_name,"topics":topics,"participant":participant,"attending":True,"atd_per":
atd_per,"fdpdate":date(2024,4,9)})
return HttpResponse(t.render(c))

URLS.py
from django.contrib import admin
from django.urls import path, re_path
from ap1.views import check_number, current_date_time from
ap1.views import four_hours_after, four_hours_before from
ap1.views import n_hours_after,display_string
from ap2.views import create_table_of_squares,vc,find_mode from
ap2.views import template_test

urlpatterns = [
path('admin/', admin.site.urls), path('cdt/',
current_date_time), path('fha/', four_hours_after),
path('fhb/', four_hours_before),
path('nha/<int:num>', n_hours_after),
path('display_string/<slug:sentence>', display_string),
re_path('check_number/(\d){1,2}/',check_number),
path('cts/<int:s>/<int:n>', create_table_of_squares),
path('vc/<str:sentence>', vc), path('find_mode/<str:listofnum>',
find_mode), path('template_test/', template_test),

Output:

6|Pag
e
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

# Create your views here.


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

URLS.py

from django.contrib import admin


from django.urls import path, re_path
from ap1.views import check_number, current_date_time from
ap1.views import four_hours_after, four_hours_before from
ap1.views import n_hours_after,display_string
from ap2.views import create_table_of_squares,vc,find_mode from
ap2.views import template_test,showlist
urlpatterns = [
path('admin/', admin.site.urls),

7|Pag
e
path('cdt/', current_date_time),
path('fha/', four_hours_after)

path('fhb/', four_hours_before),
path('nha/<int:num>', n_hours_after),
path('display_string/<slug:sentence>', display_string),
re_path('check_number/(\d){1,2}/',check_number),
path('cts/<int:s>/<int:n>', create_table_of_squares),
path('vc/<str:sentence>', vc), path('find_mode/<str:listofnum>',
find_mode), path('template_test/', template_test),
path('showlist/', showlist),

8|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:

9|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 ap1.views import check_number, current_date_time from
ap1.views import four_hours_after, four_hours_before from
ap1.views import n_hours_after,display_string
10 | P a
ge
from ap2.views import create_table_of_squares,vc,find_mode from
ap2.views import template_test,showlist,list_of_subjects
</html>
urlpatterns = [
path('admin/', admin.site.urls), path('cdt/',
current_date_time), path('fha/', four_hours_after),
path('fhb/', four_hours_before),
path('nha/<int:num>', n_hours_after),
path('display_string/<slug:sentence>', display_string),
re_path('check_number/(\d){1,2}/',check_number),
path('cts/<int:s>/<int:n>', create_table_of_squares),
path('vc/<str:sentence>', vc), path('find_mode/<str:listofnum>',
find_mode), path('template_test/', template_test),
path('showlist/', showlist),
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 forloop.counter|divisibleby:"2" %}
<tr>
<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 %}

11 | P a
ge
{% endfor %}
</table>
</body>

Output:

12 | P a
ge
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 ap1.views import check_number, current_date_time from
ap1.views import four_hours_after, four_hours_before from
ap1.views import n_hours_after,display_string
from ap2.views import create_table_of_squares,vc,find_mode from
ap2.views import template_test,showlist,list_of_subjects from ap2.views
import aboutus,home,contactus

urlpatterns = [
path('admin/', admin.site.urls), path('cdt/',
current_date_time), path('fha/', four_hours_after),
path('fhb/', four_hours_before),
path('nha/<int:num>', n_hours_after),
path('display_string/<slug:sentence>', display_string),
re_path('check_number/(\d){1,2}/',check_number),

path('cts/<int:s>/<int:n>', create_table_of_squares),
path('vc/<str:sentence>', vc),
path('find_mode/<str:listofnum>', find_mode),
path('template_test/', template_test), path('showlist/',
10 | P a g
e
DEPT. OF AIML, JNNCE,
SHIVAMOGGA
showlist), path('list_of_subjects/', list_of_subjects),
path('aboutus/', aboutus),
path('home/', home), path('contactus/',
contactus),

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>
&copy; ISE, Developed by ABC, Inc.
</footer>
</body>
</ht

10 | P a g
e
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 %}

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:

115 | P a
ge

You might also like