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

Fullstack Development Lab Programs-2

Ful stack web development lab program 2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views

Fullstack Development Lab Programs-2

Ful stack web development lab program 2
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Fullstack Development Lab Programs: Module 2

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

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:
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/',
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; 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 %}
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:
It should also display list of students registered for any selected course.
Create students and course as models with enrolment as ManyToMany field

models.py
from django.db import models

# Create your models here. class


Course(models.Model):
course_code=models.CharField(max_length=40)
course_name=models.CharField(max_length=100) course_credits=models.IntegerField()

class Student(models.Model):
student_usn=models.CharField(max_length=20)
student_name=models.CharField(max_length=100)
student_sem=models.IntegerField()
enrolment=models.ManyToManyField(Course)
reg.html inside templates folder
<html>
<body>
<form method="post" action="">
{% csrf_token %}
Student Name
<select name="sname">
{%for student in students %}
<option value="{{student.id}}">{{student.student_name}}</option>
{% endfor %}
</select><br> Course
Name
<select name="cname">
{%for course in courses %}
<option value="{{course.id}}">{{course.course_name}}</option>
{% endfor %}

</select><br>
<input type="submit" value="Enroll">
</form>
</body>
</html>

views.py
from django.http import HttpResponse from
django.shortcuts import render

from ap3.models import Course, Meeting, Student def

reg(request):
if request.method == "POST":
sid=request.POST.get("sname")
cid=request.POST.get("cname")
student=Student.objects.get(id=sid)
course=Course.objects.get(id=cid)
res=student.enrolment.filter(id=cid) if
res:
return HttpResponse("<h1>Student already enrolled</h1>")
student.enrolment.add(course)
return HttpResponse("<h1>Student enrolled successfully</h1>") else:
students=Student.objects.all()
courses=Course.objects.all()
return render(request,"reg.html",{"students":students,
"courses":courses})

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,getpos,stable
from ap3.views import insert_demo,update_demo,delete_demo,retreive_demo from
ap3.views import reg
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),
path('aboutus/', aboutus),
path('home/', home), path('contactus/',
contactus), path('getpos/', getpos),
path('stable/', stable),
path('insert_demo/', insert_demo),
path('update_demo/', update_demo),
path('delete_demo/', delete_demo),
path('retreive_demo/', retreive_demo),
path('reg/', reg),]

Database input: Insert student and courses record in


phpMyAdmin
BackEnd

If you try again, you will get

You might also like