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

FD programs-2024-07-19T00-39-30.962Z

Full stack development programs 6th sem

Uploaded by

shashankm.21ise
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 views17 pages

FD programs-2024-07-19T00-39-30.962Z

Full stack development programs 6th sem

Uploaded by

shashankm.21ise
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/ 17

1.

Develop a Django app that displays current date and time in server

In lab1 subfolder, make following changes to views.py:

from django.shortcuts import render

from django.http import HttpResponse

# Create your views here.

import datetime

def current_datetime(request):

now = datetime.datetime.now()

html = "<html><body><h1>It is now %s.</h1></body></html>" % now

return HttpResponse(html)

In project named first, make following changes to urls.py

from django.contrib import admin

from django.urls import path

from lab11.views import current_datetime

urlpatterns = [

path('cdt/', current_datetime),

2.Develop a Django app that displays date and time four hours ahead

and four hours before as an offset of current date and time in server.

In lab11 subfolder, make following changes to views.py:

from django.shortcuts import render

from django.http import HttpResponse

# Create your views here.

import datetime

def current_datetime(request):

now = datetime.datetime.now()

html = "<html><body><h1>It is now %s.</h1></body></html>" % now

return HttpResponse(html)

def four_hours_ahead(request):
dt = datetime.datetime.now() + datetime.timedelta(hours=4)

html = "<html><body><h1>After 4hour(s), it will be %s.</h1>"% (dt,)

return HttpResponse(html)

def four_hours_before(request):

dt = datetime.datetime.now() + datetime.timedelta(hours=-4)

html = "<html><body><h1>Before 4 hour(s), it was %s.</h1>"% (dt,)

return HttpResponse(html)

In project named first, make following changes to urls.py

from django.contrib import admin

from django.urls import path

from lab11.views import current_datetime,four_hours_ahead,four_hours_before

urlpatterns = [

path('cdt/', current_datetime),

path('fhrsa/',four_hours_ahead),

path('fhrsb/',four_hours_before),

3.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 ap2.views import showlist

urlpatterns = [

path('admin/', admin.site.urls),

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>

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

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 %}

{% endfor %}

</table>

</body>

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

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 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 %}

contactus.html
{% extends 'layout.html' %}

{% block title %}

Contact us

{% endblock %}

{% block content %}

<h2>Out phone: 9900923050 <br> Address:

K R Puram, Bangalore</h2>

{% endblock %}

6.Develop a Django app that performs student registration to a course. 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

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)
views.py

from django.shortcuts import render, HttpResponse

from .models import Student, Course

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

def enrollment_list(request):

students = Student.objects.all()

enrollment_data = []

for student in students:

courses = student.enrolment.all()

enrollment_data.append({

'student_name': student.student_name,
'courses': courses,

})

return render(request, "enrollment_list.html", {"enrollment_data":

enrollment_data})

In Application folder create forms.py file and include the below code

forms.py

from django import forms

from .models import Student

class StudentForm(forms.ModelForm):

class Meta:

model = Student

exclude = ['enrolment']

Under templates include two files reg.html and enrollment_list.html

reg.html

<!DOCTYPE html>

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

enrollment_list.html

<!DOCTYPE html>

<html>

<head>

<title>Enrollment List</title>

</head>

<body>

<h1>Enrollment List</h1>

<table border="1">

<thead>

<tr>

<th>Student Name</th>

<th>Enrolled Courses</th>

</tr>

</thead>

<tbody>

{% for enrollment in enrollment_data %}


<tr>

<td>{{ enrollment.student_name }}</td>

<td>

<ul>

{% for course in enrollment.courses %}

<li>{{ course.course_name }} ({{course.course_code }})</li>

{% endfor %}

</ul>

</td>

</tr>

{% endfor %}

</tbody>

</table>

</body>

</html>

admin.py

from django.contrib import admin

from .models import Student, Course

from .forms import StudentForm

class StudentAdmin(admin.ModelAdmin):

form = StudentForm

admin.site.register(Student, StudentAdmin)

admin.site.register(Course)

urls.py

from django.contrib import admin


from django.urls import path

from ap1.views import enrollment_list, reg

urlpatterns = [

path('admin/', admin.site.urls),

path('reg/', reg),

path('enrollment-list/', enrollment_list, name='enrollment_list'),

7. For student and course models created in Lab experiment for Module2, register admin interfaces,

perform migrations and illustrate data entry through admin forms.

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(blank=True, null=True)

def __str__(self):

return self.course_name

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)

def __str__(self):

return self.student_name+"("+self.student_usn+")"
admin.py

from django.contrib import admin

# Register your models here.

from ap1.models import Course, Student

# Register your models here.

#admin.site.register(Student)

admin.site.register(Course)

class StudentAdmin(admin.ModelAdmin):

list_display = ('student_name','student_usn','student_sem')

ordering=('student_name',)

search_fields = ('student_name',)

admin.site.register(Student, StudentAdmin)

8. Develop a Model form for student that contains his topic chosen for project, languages used and

duration with a model called project.

models.py

from django.db import models

from django.forms import ModelForm

# 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(blank=True, null=True)

def __str__(self):

return self.course_name
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)

def __str__(self):

return self.student_name+"("+self.student_usn+")"

class Project(models.Model):

student=models.ForeignKey(Student,on_delete=models.CASCADE)

ptopic=models.CharField(max_length=200)

plangauges=models.CharField(max_length=200)

pduration=models.IntegerField()

class ProjectReg(ModelForm):

required_css_class="required"

class Meta:

model=Project

fields=['student','ptopic','plangauges','pduration']

views.py

from django.shortcuts import render

# Create your views here.

from django.http import HttpResponse

from django.shortcuts import render

from ap1.models import ProjectReg


def add_project(request):

if request.method=="POST":

form=ProjectReg(request.POST)

if form.is_valid():

form.save()

return HttpResponse("<h1>Record inserted successfully</h1>")

else:

return HttpResponse("<h1>Record not inserted</h1>")

else:

form=ProjectReg()

return render(request,"add_project.html",{"form":form})

In your application folder, create a new folder named templates.

Inside the templates folder, create a file named add_project.html.

add_project.html

<html>

<form method="post" action="">

{% csrf_token %}

<table>

{{ form.as_table}}

<tr>

<td>

<input type="submit" value="Submit">

</td>

</tr>

</table>

</form>

</html>
urls.py

from django.contrib import admin

from django.urls import path

from ap1.views import add_project

urlpatterns = [

path('admin/', admin.site.urls),

path('add_project/', add_project),

You might also like