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 Many To Many field.
1. Create a Django project: django-admin startproject pgm5
2. Enter into Django project pgm5 : cd pgm5
3. Create a Django app: python manage.py startapp course
4. Add the app to the project: Open pgm5/settings.py and add 'course' to INSTALLED_APPS:
5. Create models.py in course app
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
6. Create forms.py in course app
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")
7. Create a templates/course directory: Create a templates/course directory inside the course
app folder
8. Create add_course.html, list_students.html, register_student.html, select_course.html inside
templates/course folder
add_course.html
<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>
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>
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>
9. Create admin.py in course app
1. from django.contrib import admin
2. from .models import Course, Student
3.
4. admin.site.register(Course)
5. admin.site.register(Student)
10. Create urls.py in course app
1. from django.urls import path
2. from . import views
3. urlpatterns = [
4. path('register/', views.register_student, name='register_student'),
5. path('students/', views.list_students, name='list_students'),
6. path('add_course/', views.add_course, name='add_course'),
7. ]
11. Create views.py in course app
1. from django.shortcuts import render, redirect
2. from .models import Student, Course
3. from .forms import CourseForm
4. from .forms import StudentForm, CourseSelectionForm
5. from django.contrib import messages
6.
7. def register_student(request):
8. if request.method == 'POST':
9. form = StudentForm(request.POST)
10. if form.is_valid():
11. student_usn = form.cleaned_data['student_usn']
12. course = form.cleaned_data['enrolment']
13. student = Student.objects.filter(student_usn=student_usn).first()
14. if student and student.enrolment.filter(id__in=course).exists():
15. messages.info(request, 'Student already registered for selected course.')
16. else:
17. form.save()
18. messages.success(request, 'Student registered successfully.')
19. return redirect('register_student')
20. else:
21. form = StudentForm()
22. return render(request, 'course/register_student.html', {'form': form})
23.
24. def list_students(request):
25. if request.method == 'POST':
26. form = CourseSelectionForm(request.POST)
27. if form.is_valid():
28. course = form.cleaned_data['course_code']
29. students = Student.objects.filter(enrolment=course)
30. return render(request, 'course/list_students.html', {'students': students, 'course': course})
31. else:
32. form = CourseSelectionForm()
33. return render(request, 'course/select_course.html', {'form': form})
34.
35. def add_course(request):
36. if request.method == 'POST':
37. form = CourseForm(request.POST)
38. if form.is_valid():
39. form.save()
40. messages.success(request, 'Course added successfully.')
41. return redirect('add_course')
42. else:
43. form = CourseForm()
44. return render(request, 'course/add_course.html', {'form': form})
45.
46.
12. Create views.py in pgm5 project
from django.contrib import admin
from django.urls import path,include
urlpatterns = [
path('admin/', admin.site.urls),
path('',include('course.urls'))
#path('', include('main.urls')),
]
13. Create database: follow below
Open command prompt and enter following command
a. Enter command - mysql --version
b. Enter command - mysql -u root –p
c. Enter root password - root
d. Enter command – create database pgm5;
14. Add Mysql Database configuration in pgm5/settings.py
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'pgm5',
'USER': 'root',
'PASSWORD': 'root',
'HOST': 'localhost',
'PORT': '3306',
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
},
}
}
15. Run following command in VS Terminal
python manage.py makemigrations
python manage.py migrate
python manage.py runserver
Output: