Full Stack Development-Lab Component-Additional Programs On Module1
Full Stack Development-Lab Component-Additional Programs On Module1
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|Page
DEPT. OF CSE , BIT BENGALURU
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
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|Page
DEPT. OF CSE , BIT BENGALURU
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|Page
DEPT. OF CSE , BIT BENGALURU
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>
4|Page
DEPT. OF CSE , BIT BENGALURU
""")
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:
5|Page
DEPT. OF CSE , BIT BENGALURU
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
6|Page
DEPT. OF CSE , BIT BENGALURU
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),
</ul>
<h1 id="i2">Ordered list of Students</h1>
<ol>
{% for student in student_names %}
<li>{{ student }}</li>
{% endfor %}
</ol>
</body>
</html>
Output:
7|Page
DEPT. OF CSE , BIT BENGALURU
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
from ap2.views import create_table_of_squares,vc,find_mode
from ap2.views import template_test,showlist,list_of_subjects
8|Page
DEPT. OF CSE , BIT BENGALURU
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),
<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 %}
{% endfor %}
</table>
</body>
</html>
Output:
9|Page
DEPT. OF AIML, JNNCE, SHIVAMOGGA
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),
10 | P a g e
DEPT. OF AIML, JNNCE, SHIVAMOGGA
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),
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>
© AIML, Developed by ABC, Inc.
</footer>
</body>
</html>
home.html
{% extends 'layout.html' %}
{% block title %}
Home
{% endblock %}
{% block content %}
<h2>This is the home page</h2>
{% endblock %}
11 | P a g e
DEPT. OF AIML, JNNCE, SHIVAMOGGA
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: Navule JNNCE</h2>
{% endblock %}
Output:
12 | P a g e