5th FSD PGM
5th FSD PGM
basicTemplate.html
<!DOCTYPE html>
<html>
<head>
<title>Student Registration</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
</head>
<body>
<form id="registrationForm">
<label for="studentName">Name:</label><br>
<label for="studentEmail">Email:</label><br>
<label for="course">Course:</label><br>
</select><br><br>
</form>
<div id="responseMessage"></div>
<script>
function submitForm() {
$.ajax({
type: 'POST',
data: formData,
success: function(response) {
$('#registrationForm')[0].reset();
},
});
</script>
</body>
</html>
models.py
class course(models.Model):
courseCode = models.CharField(max_length=10)
courseName = models.CharField(max_length=50)
courseCredits = models.IntegerField()
def __str__(self):
class student(models.Model):
usn = models.CharField(max_length=10)
name = models.CharField(max_length=40)
sem = models.IntegerField()
def __str__(self):
class projectReg(models.Model):
ptitle = models.CharField(max_length=30)
planguage = models.CharField(max_length=30)
pduration = models.IntegerField()
def __str__(self):
class projectForm(ModelForm):
required_css_class = "required"
class Meta:
model = projectReg
After writing models.py run the below commands in VS code terminal. python
manage.py makemigrations python manage.py migrate
views.py
import csv
def home(request):
def studentlist(request):
s = student.objects.all()
def courselist(request):
c = course.objects.all()
if request.method == "POST":
sid = request.POST.get("student")
cid = request.POST.get("course")
studentobj = student.objects.get(id=sid)
courseobj = course.objects.get(id=cid)
res = studentobj.courses.filter(id=cid)
if res:
resp = "<h1>Student with USN %s has already enrolled for the course %s</h1>" %
(studentobj.usn, courseobj.courseCode)
return HttpResponse(resp)
studentobj.courses.add(courseobj)
resp = "<h1>Student with USN %s successfully enrolled for the course with course
code %s</h1>" % (studentobj.usn, courseobj.courseCode)
return HttpResponse(resp)
else:
studentlist = student.objects.all()
courselist = course.objects.all()
def enrolledStudents(request):
if request.method == "POST":
cid = request.POST.get("course")
courseobj = course.objects.get(id=cid)
studentlistobj = courseobj.student_set.all()
else:
courselist = course.objects.all()
def add_project(request):
if request.method == "POST":
form = projectForm(request.POST)
if form.is_valid():
form.save()
else:
else:
form = projectForm()
class StudentListView(generic.ListView):
model = student
template_name = "GenericListViewStudent.html"
model = student
template_name = "GenericDetailedViewStudent.html"
def generateCSV(request):
courses = course.objects.all()
resp = HttpResponse(content_type="text/csv")
writer = csv.writer(resp)
for c in courses:
return resp
def generatePDF(request):
courses = course.objects.all()
resp = HttpResponse(content_type="application/pdf")
for c in courses:
table = Table(table_data)
pdf.build([table])
return resp
urls.py
urlpatterns = [
]
Output:
5.2 . Develop a search application in Django using AJAX that displays courses
enrolled by a student being searched.
basicTemplate.html
<!DOCTYPE html>
<html>
<head>
<style>
nav {
background-color: lightblue;
padding: 15px;
nav a {
background-color: #555;
nav a:hover {
margin: 0;
padding: 0;
li {
</style>
<title>
</title>
</head>
<body>
<center>
</center>
<nav>
<ul>
<li><a href="/home/">HOME</a></li>
<li><a href="/register/">REGISTER</a></li>
</ul>
</nav>
<section>
</section>
<footer>
<hr/>
<center>
© Designed and Developed by Dr. Harish Kumar B T, Dept. of CSE, BIT,
Bangalore-04
<br/>
</center>
</footer>
</body>
</html>
models.py
# Course model
class Course(models.Model):
courseCode = models.CharField(max_length=10)
courseName = models.CharField(max_length=50)
courseCredits = models.IntegerField()
def __str__(self):
# Student model
class Student(models.Model):
usn = models.CharField(max_length=10)
name = models.CharField(max_length=40)
sem = models.IntegerField()
def __str__(self):
ptitle = models.CharField(max_length=30)
planguage = models.CharField(max_length=30)
pduration = models.IntegerField()
def __str__(self):
class ProjectForm(ModelForm):
required_css_class = "required"
class Meta:
model = ProjectRegistration
After writing models.py run the below commands in VS code terminal. python
manage.py makemigrations python manage.py migrate
views.py
import csv
def home(request):
return render(request, 'home.html')
def studentlist(request):
s = student.objects.all()
def courselist(request):
c = course.objects.all()
def register(request):
if request.method == "POST":
sid = request.POST.get("student")
cid = request.POST.get("course")
studentobj = student.objects.get(id=sid)
courseobj = course.objects.get(id=cid)
res = studentobj.courses.filter(id=cid)
if res:
resp = "<h1>Student with USN %s has already enrolled for the course with code
%s</h1>" % (studentobj.usn, courseobj.courseCode)
return HttpResponse(resp)
studentobj.courses.add(courseobj)
resp = "<h1>Student with USN %s successfully enrolled for the course with code
%s</h1>" % (studentobj.usn, courseobj.courseCode)
return HttpResponse(resp)
else:
studentlist = student.objects.all()
courselist = course.objects.all()
# View function for handling the display of enrolled students for a specific course
def enrolledStudents(request):
if request.method == "POST":
cid = request.POST.get("course")
courseobj = course.objects.get(id=cid)
else:
courselist = course.objects.all()
def add_project(request):
if request.method == "POST":
form = projectForm(request.POST)
if form.is_valid():
form.save()
return HttpResponse("<h1>Project Data Successfully saved</h1>")
else:
else:
form = projectForm()
class StudentListView(generic.ListView):
model = student
template_name = "GenericListViewStudent.html"
class StudentDetailView(generic.DetailView):
model = student
template_name = "GenericDetailedViewStudent.html"
def generateCSV(request):
courses = course.objects.all()
resp = HttpResponse(content_type="text/csv")
writer = csv.writer(resp)
for c in courses:
return resp
courses = course.objects.all()
resp = HttpResponse(content_type="application/pdf")
for c in courses:
table = Table(table_data)
pdf.build([table])
return resp
urls.py
urlpatterns = [
OUTPUT: