Week8
(Aim of experiment)
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.
Solution:
Create a django project named studentcourse
Within that create an app called enrollmentapp
Include it in Installed_apps in settings
Let the database settings be default to sqlite3
Step1: Define models like Student and Course in [Link]
from [Link] import models
# Create your models here.
class Course([Link]):
name = [Link](max_length=100)
def __str__(self):
return [Link]
class Student([Link]):
name = [Link](max_length=100)
usn = [Link](max_length=10, default="1JT21IS000")
courses = [Link](Course)
def __str__(self):
return [Link]()
Step2: Add a [Link] in the enrollmentapp
In it have this:
from django import forms
from .models import Student, Course
class StudentForm([Link]):
class Meta:
model = Student
fields = '__all__'
Step3: [Link] in app
from [Link] import admin
from [Link] import path
from . import views
urlpatterns = [
path('student_list/',[Link].as_view(),name='student_list'),
path('student_detail/<int:pk>/',[Link].as_view(),name='st
udent_detail'),
path('register_student/',[Link].as_view(),name='registe
r_student'),
]
Step4: [Link] in project
from [Link] import admin
from [Link] import path, include
urlpatterns = [
path('admin/', [Link]),
path('', include('[Link]')),
]
Step5: Register the models in the admin site
from [Link] import admin
from .models import *
# Register your models here.
[Link](Student)
[Link](Course)
Step6: [Link] demonstrates the concept of generic views like templateview, detailview etc
from [Link] import render, redirect
from [Link] import StudentForm
from [Link] import View
from [Link] import(
#display
TemplateView,
ListView,
DetailView,
#edit
FormView,
CreateView,
UpdateView,
DeleteView
)
# Create your views here.
from .models import Course, Student
#Either this can be the handler for reg student
def register_student(request):
if [Link]:
form = StudentForm([Link])
if form.is_valid():
[Link]()
else:
form = StudentForm()
return
render(request,'enrollmentapp/register_student.html',{'form':form})
class StudentDetailView(DetailView):
model = Student
template_name = 'enrollmentapp/student_detail.html'
context_object_name = 'students'
# Create your views here.
class StudentListView(ListView):
template_name = 'enrollmentapp/student_list.html'
model = Student
context_object_name = 'studentlist'
#or this can be the handler for reg student
class RegisterStudentView(FormView):
template_name = 'enrollmentapp/register_student.html'
form_class = StudentForm
success_url = '/'
fields = ['name','usn','courses']
def form_valid(self, form):
# This method is called when valid form data has been POSTed.
# It should return an HttpResponse.
[Link]()
return super().form_valid(form)
Step7: Create three files Register_student.html, Student_list.html and Student_details.html in
templates/enrollmentapp folder of app which you have to create
Register_student.html
<html>
<body>
<form method="post">
{% csrf_token %}
<table>
{{form.as_table}}
</table>
<input type="submit" value="Submit">
</form>
</body>
</html>
Student_list.html
<html>
<body>
<ul>
<h1>Student List</h1>
{% for student in studentlist %}
<li><a href="{% url 'student_detail'
pk=[Link]%}">{{[Link]}}</a></li>
{% endfor %}
</ul>
</body>
</html>
Student_details.html
<html>
<body>
<h1>Student Details</h1>
<h1>{{[Link]}}</h1>
<br>
<h2>{{[Link]}}</h2><br>
</body>
</html>
(Upper case / Lower case in file names to be decided with due diligence)
Step8: py [Link] makemigrations
Py [Link] migrate
Step9: py [Link] createsuperuser
To create super user to enter the courses and register students from the backend.
Final Folder Structure should be
Step10: py manage,py runserver
From admin interface enter a few courses and enroll students into them.
Then visit the site:
Click on a student should show you their details: