0% found this document useful (0 votes)
20 views3 pages

P 7

Uploaded by

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

P 7

Uploaded by

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

--------------------for reistering course----------------------------

--> create templates folder in the app


--- create reg.html file in the templates folder

reg.html

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

view.html
from django.http import HttpResponse
from django.shortcuts import render

from app7.models import Course, 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 app7.views import reg
urlpatterns = [
path('admin/', admin.site.urls),
path('reg/', reg),
]

----> create models.py file in the app


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)

----------------------------for displaying------------------------

course_search.html

<html>
<body>
<form method="POST" action="">
courses
{% csrf_token %}
<select name="cname">
{%for course in courses %}
<option value="{{course.id}}">{{course.course_name}}</option>
{% endfor %}
</select>
<input type="submit" value="Search">
</form>
</body>
</html>

selected_student.html

<html>
<body>
<table border>
<tr>
<th> Student Name</th>
<th> Student USN</th>
<th>Sem</th>
</tr>
{% for student in student_list %}
<tr>
<td>{{student.student_name}}</td>
<td>{{student.student_usn}}</td>
<td>{{student.student_sem}}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
views.py
def course_search(request):
if request.method=="POST":
cid=request.POST.get("cname")
s=Student.objects.all()
student_list=list()
for student in s:
if student.enrolment.filter(id=cid):
student_list.append(student)
if len(student_list)==0:
return HttpResponse("<h1> No Students enrolled</h2>")
return render(request,"selected_students.html",
{"student_list":student_list})
else:
courses=Course.objects.all()
return render(request, "course_search.html",{"courses":courses})

urls.py
from django.contrib import admin
from django.urls import path, re_path
from app7.views import reg, course_search
urlpatterns = [
path('admin/', admin.site.urls),
path('reg/', reg),
path('course_search/', course_search),
]

You might also like