3. 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 Many To Many
field.
1. Create a Django app named course
python manage.py startapp course
2. Add the app to your project settings
myproject/settings.py and add 'course' to the INSTALLED_APPS list
INSTALLED_APPS = [
...
'course',
]
3. Create Models in course/models.py
# Create your models here.
from django.db import models
class Course(models.Model):
course_code = models.CharField(max_length=10)
course_name = models.CharField(max_length=100)
course_credits = models.IntegerField()
def str (self):
return self.course_name
class Student(models.Model):
student_usn = models.CharField(max_length=10)
student_name = models.CharField(max_length=100)
student_sem = models.IntegerField()
enrolment = models.ManyToManyField(Course)
def str (self):
return self.student_name
4. Create Forms in course/forms.py
from django import forms
from .models import Student, Course
class CourseForm(forms.ModelForm):
class Meta:
model = Course
fields = ['course_code', 'course_name', 'course_credits']
class StudentForm(forms.ModelForm):
class Meta:
model = Student
fields = ['student_usn', 'student_name', 'student_sem', 'enrolment']
class CourseSelectionForm(forms.Form):
course_code = forms.ModelChoiceField(queryset=Course.objects.all(),
label="Select Course")
5. Create Views in course/views.py
# Create your views here.
from django.shortcuts import render, redirect
from .models import Student, Course
from .forms import CourseForm
from .forms import StudentForm, CourseSelectionForm
from django.contrib import messages
def register_student(request):
if request.method == 'POST':
form = StudentForm(request.POST)
if form.is_valid():
student_usn = form.cleaned_data['student_usn']
course = form.cleaned_data['enrolment']
student = Student.objects.filter(student_usn=student_usn).first()
if student and student.enrolment.filter(id in=course).exists():
messages.info(request, 'Student already registered for selected course.')
else:
form.save()
messages.success(request, 'Student registered successfully.')
return redirect('register_student')
else:
form = StudentForm()
return render(request, 'course/register_student.html', {'form': form})
def list_students(request):
if request.method == 'POST':
form = CourseSelectionForm(request.POST)
if form.is_valid():
course = form.cleaned_data['course_code']
students = Student.objects.filter(enrolment=course)
return render(request, 'course/list_students.html', {'students': students,
'course': course})
else:
form = CourseSelectionForm()
return render(request, 'course/select_course.html', {'form': form})
def add_course(request):
if request.method == 'POST':
form = CourseForm(request.POST)
if form.is_valid():
form.save()
messages.success(request, 'Course added successfully.')
return redirect('add_course')
else:
form = CourseForm()
return render(request, 'course/add_course.html', {'form': form})
6. Create Templates: in course/templates/course/
register_student.html
<!DOCTYPE html>
<html>
<head>
<title>Register Student</title>
</head>
<body>
<h1>Register Student</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Register</button>
</form>
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
</body>
</html>
select_course.html
<!DOCTYPE html>
<html>
<head>
<title>Select Course</title>
</head>
<body>
<h1>Select Course to List Students</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Show Students</button>
</form>
</body>
</html>
list_students.html
<!DOCTYPE html>
<html>
<head>
<title>List of Students</title>
</head>
<body>
<h1>Students Registered for {{ course.course_name }}</h1>
<ul>
{% for student in students %}
<li>{{ student.student_name }} ({{ student.student_usn }})</li>
{% endfor %}
</ul>
</body>
</html>
add_course.html
<!DOCTYPE html>
<html>
<head>
<title>Add Course</title>
</head>
<body>
<h1>Add Course</h1>
<form method="post">
{% csrf_token %}
{{ form.as_p }}
<button type="submit">Add Course</button>
</form>
{% if messages %}
<ul>
{% for message in messages %}
<li>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
</body>
</html>
7. Add URLs in course/urls.py
from django.urls import path
from . import views
urlpatterns = [
path('register/', views.register_student, name='register_student'),
path('students/', views.list_students, name='list_students'),
path('add_course/', views.add_course, name='add_course'),
]
8. Create course/admin.py for Django admin user
# Register your models here.
from django.contrib import admin
from .models import Course, Student
admin.site.register(Course)
admin.site.register(Student)
9. Add Mysql Database configuaration in myproject/settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'myproject',
'USER': 'root',
'PASSWORD': '',
'HOST': 'localhost',
'PORT': '3306',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
},
}
}
10. Start Mysql Server from XAMPP Control Panel
11. Run Migrations in Visual Code Terminal
python manage.py makemigrations
python manage.py migrate
12. Run Django server
python manage.py runserver
13. Visit your app in your web browser
http://127.0.0.1:8000/course/add_course
http://127.0.0.1:8000/course/register
http://127.0.0.1:8000/course/students
Output (Snapshots)