0% found this document useful (0 votes)
16 views37 pages

21CS62-Fullstack Laboratorynew1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views37 pages

21CS62-Fullstack Laboratorynew1

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

VIVEKANANDA INSTITUTE OF TECHNOLOGY

Department Of Computer Science and Engineering

FULL STACK

LABORATORY (21CS62)

Sixth Sem CSE

2024-2025

Department of Computer Science and Engineering

Prepared By

Dr. Vidya A., HoD/CSE

Sijin P., AP/CSE

Suresh H K ., CSE
1. Develop a Django app that displays current date and time in server

//Terminal

C:\Djangoprojects> django-admin startproject program1


C:\Djangoprojects\program1> django-admin startapp program1_app

//program1/templates/index.html

<html>
<body>
<center>
<h1>Display of Date and Time using Django</h1>
<h2>{% now 'd/m/y, h:i:s' %}</h2>
</center>
</body>
</html>

//program1/program1_app/views.py

from django.shortcuts import render


import datetime
# Create your views here.
def showdatetime(request):
now=datetime.datetime.now()
return render(request,'index.html')

//program1/program1/urls.py

from django.contrib import admin


from django.urls import path
from program1_app import views

urlpatterns = [
path('admin/', admin.site.urls),
path('',views.showdatetime)
]
Output:

C:\Djangoprojects\program1> python manage.py runserver

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.

//Terminal

C:\Djangoprojects> django-admin startproject program2


C:\Djangoprojects\program2> django-admin startapp program2_app

program2/templates/index1.html

<html>
<body>
<center>
<h1>Display of date and time according to an offset</h1>
<h2>The time four hours ahead:{{time1}}</h2>
<h2>The time four hours behind:{{time2}}</h2>
</center>
</body>
</html>

program2/program2_app/views.py

from django.shortcuts import render


from datetime import datetime, timedelta
import urllib.request

def timechange(request):
# def timechangeadd(request):
time1=datetime.now()+timedelta(hours=4)
# def timechangesub(request):
time2=datetime.now()-timedelta(hours=4)
return render(request,"index1.html",{'time1':time1,'time2':time2})

program2/program2/urls.py

from django.contrib import admin


from django.urls import path
from program2_app import views
urlpatterns = [
path('admin/', admin.site.urls),
path('', views.timechange)
]

//Output
C:\Djangoprojects\program2> python manage.py runserver
3. Develop a simple Django app that displays an unordered list of fruits.

//Terminal
C:\Djangoprojects> django-admin startproject program3
C:\Djangoprojects\program3> django-admin startapp fruits_app

//program3/templates/fruittemplate.html

{% comment %}{% load static%}{% endcomment %}


<html>
<head>
<style>
body
{
background-color: LemonChiffon;
}

</style>
</head>
{% comment %} <body id="bg" style="background-image:url('{% static
"images/fruitsbg.jpeg" %}')">{% endcomment %}
<body>
<center>
<h1>The Fruit Chart</h1>
<hr></hr>
{% comment %} <img src="{% static 'images/fruits1.jpeg' %}">
<img src="{% static 'images/fruits2.jpeg' %}">
<img src="{% static 'images/fruits3.jpeg' %}"> {% endcomment %}
</center>
{% for fruitobj in fruits %}
{% if fruitobj.quality %}
<ul>
<li>{{fruitobj.fruitname}}</li>
</ul>
{% endif %}
{% endfor %}

</body>
</html>

//program3/templates/studenttemplate.html

{% comment %}{% load static %} {% {% endcomment %}


<html>
{% comment %} <body class="bg" style="background-image:url('{% static
"images/vkit1.jpeg" %}')"> {% endcomment %}
<body>
<h1>The selected students for Smart India Hackethon 2024</h1>
<hr></hr>
<ol>
{% for obj in students %}
{% if obj.attendance %}
<li>{{obj.studentname}}</li>
{% endif %}
{% endfor %}
</ol>
</body>
</html>

//program3/fruits_app/views.py

from django.shortcuts import render

# Create your views here.


def Fruits(request):
fruit_set={
'fruits':[
{
'fruitname':'Apple',
'quality':True
},
{
'fruitname':'Orange',
'quality':True
},
{
'fruitname':'Grapes',
'quality':False
},
{
'fruitname':'Banana',
'quality':False
},
{
'fruitname':'Pomegranate',
'quality':True
},
{
'fruitname':'Kiwi',
'quality':True
},
{
'fruitname':'Watermelon',
'quality':True
},
{
'fruitname':'Cherry',
'quality':True
},
]
}
return render(request,'fruittemplate.html',fruit_set)

def Students(request):
student_set={
'students':[
{
'studentname':'Rajeev',
'college':'VKIT',
'attendance':True
},
{
'studentname':'Hameed',
'college':'DBIT',
'attendance':True
},
{
'studentname':'Sheela',
'college':'DBIT',
'attendance':True
},
{
'studentname':'Mahesh',
'college':'JIT',
'attendance':False
},
{
'studentname':'Mala',
'college':'VKIT',
'attendance':True
}
]
}
return render(request,'studenttemplate.html',student_set)

//program3/ program3/urls.py

from django.contrib import admin


from django.urls import path
from fruits_app import views

urlpatterns = [
path('admin/', admin.site.urls),
path('', views.Fruits),
path('', views.Students)
]

//Output

C:\Djangoprojects\program3> python manage.py runserver

4. 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.
//Terminal

C:\Djangoprojects> django-admin startproject program4


C:\Djangoprojects\program4> django-admin startapp program4__app

// program4/templates/about/layout.html

<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
}

li {
display: inline;
}
</style>
</head>
<center>
<body style="background-color:grey;">
<h1>About Us</h1>
<p> It is a Django based website designing company,
It is a Django based website designing company,
It is a Django based website designing company, </p>
</body>
</center>
</html>

// program4/templates/contact/layout.html

<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
}

li {
display: inline;
}
</style>
</head>
<center>
<body style="background-color:grey;">
<h1>About Us</h1>
<p>
Contact us at <a href=" weblog.canva.com"> weblog.canva.com</a>
ph:9998887777

</p>
</body>
</center>
</html>

// program4/templates/home/layout.html

<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
}

li {
display: inline;
}
</style>
</head>
<center>
<body style="background-color:grey;">
<h1>Website Design Center</h1>
<ol>
<li><a href="{% url 'layout1' %}">Home</a></li>
<li><a href="{% url 'layout2' %}">About</a></li>
<li><a href="{% url 'layout3' %}">Contact</a></li>
</ol>
</body>
</center>
</html>

// program4/templates/layout.html

<html>
<head>
<style>
ul {
list-style-type: none;
margin: 0;
padding: 0;
}
li {
display: inline;
}
</style>
</head>
<center>
<body style="background-color:grey;">
<h1>Website Design Center</h1>
<ol>
<li><a href="{% url 'layout1' %}">Home</a></li>
<li><a href="{% url 'layout2' %}">About</a></li>
<li><a href="{% url 'layout3' %}">Contact</a></li>
</ol>
</body>
</center>
</html>

// program4/program4/urls.py

from django.contrib import admin


from django.urls import path, include

urlpatterns = [
path('admin/', admin.site.urls),
path('',include('home_app.urls')),
path('',include('about_app.urls')),
path('',include('contact_app.urls'))
]

// program4/contact_app/urls.py

from django.contrib import admin


from django.urls import path, include
from . import views

urlpatterns = [
path('contact_page',views.layout,name="layout3")
]

// program4/about_app/urlspy

from django.contrib import admin


from django.urls import path, include
from . import views

urlpatterns = [
path('about_page',views.layout,name="layout2")
]
// program4/home_app/urls.py

from django.contrib import admin


from django.urls import path, include
from . import views

urlpatterns = [
path('',views.home,name="home"),
path('home_page',views.layout,name="layout1")
]

// program4/about_app/views.py

from django.shortcuts import render

# Create your views here.


def layout(request):
return render(request,'about/layout.html')

// program4/contact_app/views.py

from django.shortcuts import render

# Create your views here.


def layout(request):
return render(request,'contact/layout.html')

// program4/home_app/views.py

from django.shortcuts import render

# Create your views here.


def home(request):
return render(request,'layout.html')

def layout(request):
return render(request,'home/layout.html')

//Output
C:\Djangoprojects\program4> python manage.py runserver
5. 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.
//Terminal

C:\Djangoprojects> django-admin startproject program5


C:\Djangoprojects\program5n1> django-admin startapp admission_app

// program5/templates/admission.html

<html>
<body>
<h1> Admission</h1>
<form method="post" enctype="multipart/form-data">
{% csrf_token %}
name:<input type="text" name="name"/>
cource:<input type="text" name="cource"/>
<button type="submit"> save</button>
</form>

</body>
</html>

// program5/templates/display.html

<html>
<body>
<h1>The selected students from Electronics and communication
engineering</h1>
<ol>
{% for student_obj in cources %}
{% if student_obj.cource == "ec" %}
<li>{{student_obj.name}}</li>
{% endif %}
{% endfor %}
</ol>
</body>
</html>

// program5/admission_app/urls.py

from django.urls import path


from . import views

urlpatterns = [
path('create/', views.create,name='create'),
path('display/', views.display,name='display')
]

// program5/admission_app/views.py

from django.shortcuts import render


from . models import CourceInfo1

# Create your views here.


def create(request):
if request.POST:
name=request.POST.get('name')
cource=request.POST.get('cource')
student_obj=CourceInfo1(name=name, cource=cource)
student_obj.save()

return render(request,'admission.html')

def display(request):
studentcource=CourceInfo1.objects.all()

return render(request,'display.html',{'cources':studentcource})

// program5/program5/urls.py

from django.contrib import admin


from django.urls import path,include

urlpatterns = [
path('admin/', admin.site.urls),
path('',include('admission_app.urls'))
]

// program5/admission_app/models.py

from django.db import models

# Create your models here.


class CourceInfo1(models.Model):
name=models.CharField(max_length=250)
cource=models.CharField(max_length=250)
//Output

C:\Djangoprojects\program5>python manage.py makemigrations


admission_app
C:\Djangoprojects\program5> python manage.py migrate admission_app
C:\Djangoprojects\program5> python manage.py migrate --run-syncdb
C:\Djangoprojects\program5> python manage.py runserver

6. For student and course models created in Lab experiment for Module2,
register admin interfaces, perform migrations and illustrate data entry
through admin forms.

//Terminal
C:\Djangoprojects> django-admin startproject program6
C:\Djangoprojects\program6> django-admin startapp program6_app
//program6/program6_app/views.py

from django.shortcuts import render


from . models import CourceInfo2

# Create your views here.


def create(request):
if request.POST:
name=request.POST.get('name')
cource=request.POST.get('cource')
student_obj=CourceInfo2(name=name, cource=cource)
student_obj.save()

return render(request,'admission.html')

def display(request):
studentcource=CourceInfo2.objects.all()

return render(request,'display.html',{'cources':studentcource})

//program6/program6_app/urls.py

from django.urls import path


from . import views

urlpatterns = [

path('create/', views.create,name='create'),
path('display/', views.display,name='display')
]

//program6/program6_app/models.py

from django.db import models

# Create your models here.


class CourceInfo2(models.Model):
name=models.CharField(max_length=250)
cource=models.CharField(max_length=250)

//program6/program6_app/admin.py

from django.contrib import admin


from program6_app.models import CourceInfo
# Register your models here.
admin.site.register(CourceInfo2)
//program6/program6/urls.py

from django.contrib import admin


from django.urls import path,include

urlpatterns = [
path('admin/', admin.site.urls),
path('',include('program6_app.urls'))
]

//Output

PS C:\Djangoprojects\program6> python manage.py makemigrations program6_app


PS C:\Djangoprojects\program6> python manage.py migrate program6_app
PS C:\Djangoprojects\program6> python manage.py migrate --run-syncdb
PS C:\Djangoprojects\program6> python manage.py createsuperuser
PS C:\Djangoprojects\program6> python manage.py runserver
7. Develop a Model form for student that contains his topic chosen for
project, languages used and duration with a model called project.

//Terminal
C:\Djangoprojects> django-admin startproject program7
C:\Djangoprojects\program7> django-admin startapp student_app

//program7/program7/student_app/student_form.html

<html>
<body>
<center>
<h1> Student Information</h1>
</center>
<form method="POST">
{% csrf_token %}
{{form.as_table}}
<input type="submit" value="submit"><br>
<h2>Student names</h2>
{% for student_app in student_apps%}
{{student_app.name}},
{% endfor%}
<hr></hr>
{% comment %} <input type="submit" value="submit"> {% endcomment %}
<h2>Student details</h2>
{% for student_app in student_apps%}
{% if student_app.cource == "cs" %}
{{student_app.name}}, {{student_app.cource}}, {{student_app.topic}},
{{student_app.language}}, {{student_app.duration}}<br>
{% endif %}
{% endfor%}
<hr>
</form>

</body>
</html>

//program7/student_app/models.py

from django.db import models

# Create your models here.


class student(models.Model):
name=models.CharField(max_length=250)
cource=models.CharField(max_length=200)
topic=models.CharField(max_length=200)
language=models.CharField(max_length=200)
duration=models.CharField(max_length=200)
def __str__(self):
return self.name

//program7/student_app/views.py

from django.urls import reverse_lazy


from django.shortcuts import render
from django.views.generic import CreateView
from student_app .models import student
# Create your views here.
class studentCreateView(CreateView):
model=student
fields='__all__'
success_url=reverse_lazy('home')

def get_context_data(self, **kwargs):


context=super().get_context_data()
context['student_apps']=student.objects.all
return context

//program7/student_app/admin.py

from django.contrib import admin


from student_app.models import student

# Register your models here.


admin.site.register(student)

//program7/program7/urls.py

from django.contrib import admin


from django.urls import path,include

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('student_app.urls'))
]

//program7/student_app/urls.py

from django.urls import path,reverse_lazy


from django.views.generic import CreateView
from student_app.models import student
from student_app.models import student
from . import views

urlpatterns = [
# path('',CreateView.as_view(model=student,
fields='__all__',success_url=reverse_lazy('home')),name='home')
path('',views.studentCreateView.as_view(),name='home')
]

//Output
PS C:\Djangoprojects\program7> python manage.py makemigrations student_app
PS C:\Djangoprojects\program7> python manage.py migrate student_app
PS C:\Djangoprojects\program7> python manage.py migrate --run-syncdb
PS C:\Djangoprojects\program7> python manage.py createsuperuser
PS C:\Djangoprojects\program7> python manage.py runserver

8. For students enrolment developed in Module 2, create a generic class view


which displays list of students and detail view that displays student details
for any selected student in the list.

//Terminal
C:\Djangoprojects> django-admin startproject program8
C:\Djangoprojects\program8> django-admin startapp program8_app

//program8/templates/program8_app/student_form.html

<html>
<body>
<center>
<h1> Student Information</h1>
</center>
<form method="POST">
{% csrf_token %}
{{form.as_table}}
<input type="submit" value="submit"><br>
<h2>Student names</h2>
{% for student_app in student_apps%}
{{student_app.name}},
{% endfor%}
<hr></hr>
{% comment %} <input type="submit" value="submit"> {% endcomment %}
<!---- <h2>Student details</h2>
{% for student_app in student_apps%}
{% if student_app.name == "Basavara" %}
{{student_app.name}}, {{student_app.cource}}, {{student_app.topic}},
{{student_app.language}}, {{student_app.duration}}<br>
{% endif %}
{% endfor%}
<hr>-->
</form>
</body>
</html>

//program8/templates/display.html

<html>
<body>
<h1>The student detail is</h1>
<ol>
{% for student_obj in cources %}
{% if student_obj.id == 1 %}
<li>{{student_obj.name}}, {{student_obj.cource}}, {{student_obj.topic}},
{{student_obj.language}}, {{student_obj.duration}}</li>
{% endif %}
{% endfor %}
</ol>
</body>
</html>

//program8/program8_app/models.py

from django.db import models

# Create your models here.


class student(models.Model):
name=models.CharField(max_length=250)
cource=models.CharField(max_length=200)
topic=models.CharField(max_length=200)
language=models.CharField(max_length=200)
duration=models.CharField(max_length=200)
def __str__(self):
return self.name

//program8/program8_app/views.py

from django.urls import reverse_lazy


from django.shortcuts import render
from django.views.generic import CreateView
from program8_app .models import student
from program8_app.forms import MyForm
from . models import student

# Create your views here.


class studentCreateView(CreateView):
model=student
#fields='__all__'
form_class=MyForm
success_url=reverse_lazy('home')

def get_context_data(self, **kwargs):


context=super().get_context_data()
context['student_apps']=student.objects.all
return context

def display(request):
studentcource=student.objects.all()

return render(request,'display.html',{'cources':studentcource})

//program8/program8_app/admin.py

from django.contrib import admin


from program8_app.models import student

# Register your models here.


admin.site.register(student)

//program8/program8_app/forms.py
from django import forms
from program8n1_app.models import student

class MyForm(forms.ModelForm):

class Meta:
model = student
fields = '__all__'
# widgets = {'cource': forms.HiddenInput(), 'topic': forms.HiddenInput(),
'language': forms.HiddenInput(),'duration': forms.HiddenInput()}

//program8/program8_app/urls.py

from django.urls import path,reverse_lazy


from django.views.generic import CreateView
from program8_app.models import student
from . import views

# urlpatterns = [
# path('',CreateView.as_view(model=student,
fields='__all__',success_url=reverse_lazy('home')),name='home')
#]

urlpatterns = [
# path('',CreateView.as_view(model=student,
fields='__all__',success_url=reverse_lazy('home')),name='home')
path('',views.studentCreateView.as_view(),name='home'),
path('display/', views.display,name='display')
]

//program8/program8/urls.py

from django.contrib import admin


from django.urls import path,include

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('program8_app.urls'))
]
//Output
PS C:\Djangoprojects\program8> python manage.py makemigrations 'program8_app
PS C:\Djangoprojects\program8> python manage.py migrate 'program8_app
PS C:\Djangoprojects\program8> python manage.py migrate --run-syncdb
PS C:\Djangoprojects\program8> python manage.py createsuperuser
PS C:\Djangoprojects\program8> python manage.py runserver

9. a. Develop example Django app that performs CSV and PDF generation
for any models created in previous laboratory component.

//Terminal
C:\Djangoprojects> django-admin startproject program9
C:\Djangoprojects\program9> django-admin startapp program9_app

//program9/program9_app/templates/index.html

<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>PDF</title>
</head>
<body>
<h1>Hello, world!</h1>
<a href="{% url 'pdf' %}">Download<a>
</body>
</html>

//program9/program9_app/templates/report.html

<html>
<body>
<h1>VKIT, Bengaluru...</h1>
</body>
</html>

//program9/program9_app/views.py

from django.shortcuts import render


from django.http import HttpResponse
from django.views.generic import View
from .utils import render_to_pdf

# Create your views here.


def index(request):
return render(request,'index.html')
class GeneratePdf(View):
def get(self,request,*args,**kwargs):
pdf=render_to_pdf('report.html')
return HttpResponse(pdf, content_type='application/pdf')

//program9/program9_app/utils.py
from io import BytesIO
from django.http import HttpResponse
from django.template.loader import get_template
from xhtml2pdf import pisa
def render_to_pdf(template_src,context_dict={}):
template=get_template(template_src)
html=template.render(context_dict)
result=BytesIO()
pdf=pisa.pisaDocument(BytesIO(html.encode("ISO-8859-1")),result)
if not pdf.err:
return HttpResponse(result.getvalue(),content_type='application/pdf')
return None

//program9/program9_app/urls.py

from django.urls import path


from . import views
from .views import GeneratePdf
urlpatterns = [
path('',views.index,name='index'),
path('pdf/',GeneratePdf.as_view(),name='pdf')
]

//program9/program9/urls.py

from django.contrib import admin


from django.urls import path,include

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('program9_app.urls'))
]

//Output
PS C:\Djangoprojects\program9> python manage.py createsuperuser
PS C:\Djangoprojects\program9> python manage.py runserver
9. b. Develop example Django app that performs CSV and PDF generation
for any models created in previous laboratory component.

//Terminal
C:\Djangoprojects> django-admin startproject program9b
C:\Djangoprojects\program9b> django-admin startapp program9b_app

//program9b/templates/index.html

{% load static %}
<html>
<body>
<center>
<h1>HTML to CSV conversion of Program 5 model results</h1>
<a href="{% url 'index' %}">Download<a>
</center>
</body>
</html>

//program9b/program9b_app/model.py

from django.db import models

# Create your models here.


class Member(models.Model):
name=models.CharField(max_length=50)
cource=models.CharField(max_length=50)

//program9b/program9b_app/view.py

from django.shortcuts import render


from django.http import HttpResponse
import csv
from .models import Member
# Create your views here.
def home(request):
return render(request,'index.html')
def index(request):
response=HttpResponse(content_type='text/csv')
writer=csv.writer(response)
writer.writerow(['Name','Cource'])
for member in Member.objects.all().values_list('name','cource'):
writer.writerow(member)
response['Content-Disposition']='attachment; filename="members.csv"'
return response

//program9b/program9b_app/url.py

from django.urls import path


from . import views
urlpatterns = [
path('index/',views.index,name='index'),
path('',views.home,name='home'),
]

//program9b/program9b/url.py
from django.contrib import admin
from django.urls import path,include

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('program9b_app.urls'))
]

//Output
PS C:\Djangoprojects\program9b> python manage.py makemigrations
'program9b_app
PS C:\Djangoprojects\ program9b > python manage.py migrate 'program9b_app
PS C:\Djangoprojects\ program9b > python manage.py migrate --run-syncdb
PS C:\Djangoprojects\ program9b > python manage.py createsuperuser
PS C:\Djangoprojects\ program9b > python manage.py runserver

10. Develop a registration page for student enrolment as done in Module 2


but without page refresh using AJAX.

//Terminal
C:\Djangoprojects> django-admin startproject program9b
C:\Djangoprojects\program9b> django-admin startapp program9b_app

//program10/program10_app/urls.py

from django.urls import path


from . import views

urlpatterns = [

path('create/', views.create,name='create'),
path('display/', views.display,name='display')
]
//program10/program10_app/models.py

from django.db import models

# Create your models here.


class CourceInfo1(models.Model):
name=models.CharField(max_length=250)
cource=models.CharField(max_length=250)

//program10/program10_app/views.py

from django.shortcuts import render


from django.http import JsonResponse
from . models import CourceInfo1

# Create your views here.


def create(request):
if request.POST:
name=request.POST.get('name')
cource=request.POST.get('cource')
student_obj=CourceInfo1(name=name, cource=cource)
student_obj.save()

if (name==""):
# Process the form data here...
return JsonResponse({'success': False})
else:
return JsonResponse({'success': True})
else:

return render(request,'admission.html')

def display(request):
studentcource=CourceInfo1.objects.all()

return render(request,'display.html',{'cources':studentcource})

//program10/program10/urls.py

from django.contrib import admin


from django.urls import path,include

urlpatterns = [
path('admin/', admin.site.urls),
path('',include('program10_app.urls'))
]

//program10/templataes/admission.html
{% load static %}
<html>
<body>
<h1> Admission</h1>
<form id="contact" method="post" enctype="multipart/form-data">
{% csrf_token %}
name:<input type="text" name="name"/>
cource:<input type="text" name="cource"/>
<button type="submit"> save</button>
</form>
<div id="alert" style="display:none;"></div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(function() {
$('#contact').on('submit', function(event) {
event.preventDefault();
$.ajax({
url: '{% url "create" %}',
type: 'POST',
data: $(this).serialize(),
dataType: 'json',
success: function(response) {
if (response.success) {
$('contact').hide();
$('#alert').html('<div class="alert alert-success">Your details are added,
Thank you</div>').show();
} else {
$('#alert').html('<div class="alert alert-danger">' + response.errors +
'</div>').show();
}
}
});
});
});
</script>
</body>
</html>

//program10/templataes/display.html

<html>
<body>
<h1>The student details</h1>
<ol>
{% for student_obj in cources %}
{% if student_obj.cource != null %}
<li>{{student_obj.name}}, {{student_obj.cource}}</li>
{% endif %}
{% endfor %}
</ol>
</body>
</html>

//Output
PS C:\Djangoprojects\program10> python manage.py makemigrations
'program10_app
PS C:\Djangoprojects\program10> python manage.py migrate 'program10_app
PS C:\Djangoprojects\program10> python manage.py migrate --run-syncdb
PS C:\Djangoprojects\program10> python manage.py createsuperuser
PS C:\Djangoprojects\program10> python manage.py runserver

11. Develop a search application in Django using AJAX that displays courses
enrolled by a student being searched.

//program11n1/program11n1/urls.py

from django.contrib import admin


from django.urls import path,include

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('program11n1_app.urls'))
]

//program11n1/program11n1_app/admin.py

from django.contrib import admin


from program11n1_app.models import Searchdb
# Register your models here.
admin.site.register(Searchdb)

//program11n1/program11n1_app/models.py

from django.db import models

# Create your models here.


class Searchdb(models.Model):
name=models.CharField(max_length=200)

def __str__(self):
return self.name

//program11n1/program11n1_app/urls.py

from django.urls import path


from django.contrib import admin
from . import views

urlpatterns = [

path('searchq/',views.BlogSearchView.as_view(),name='search1'),
]

//program11n1/program11n1_app/views.py

from django.db.models.query import QuerySet


from django.shortcuts import render
from django.views.generic import ListView,DetailView, CreateView
from .models import Searchdb
import json
# Create your views here.
class BlogSearchView(ListView):
model=Searchdb
template_name='search.html'
context_object_name='posts'

def get_queryset(self):
searchquery=self.request.GET.get('search')
return Searchdb.objects.filter(name__icontains=searchquery)
//program11n1/templates/search.html

{% load static %}
<html>
<body>
<h1> VKIT SEARCH PROCESS</h1>
<hr>/</hr>
<form action="{% url 'search1' %}" method="get">
{% csrf_token %}
<h2> Enter the query according to the given context</h2>
<input type="text" name="search" placeholder="search"><br>
<button type="submit">Find</button></br>
{% for post in posts %}
{{post.name}}</br>
{% endfor %}

</body>
</html>

//Output
PS C:\Djangoprojects\program11n1> python manage.py makemigrations
'program11n1_app
PS C:\Djangoprojects\program11n1> python manage.py migrate 'program10_app
PS C:\Djangoprojects\program11n1> python manage.py migrate --run-syncdb
PS C:\Djangoprojects\program11n1> python manage.py createsuperuser
PS C:\Djangoprojects\program11n1> python manage.py runserver

You might also like