Make the changes in register.html
and views.py
file.
<!DOCTYPE html>
<head>
<title>Register Student to Course</title>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
</head>
<body>
<h2>Register Student</h2>
<form id="registrationForm" method="post" action="{% url 'register' %}">
{% csrf_token %}
<label for="name">Name:</label>
<input type="text" id="name" name="name" maxlength="50" required>
<br>
<label for="email">Email:</label>
<input type="email" id="email" name="email" required>
<br>
<label for="course">Select Course:</label>
<select id="course" name="course" required>
{% for course in courses %}
<option value="{{ course.id }}">{{ course.title }}</option>
{% endfor %}
</select>
<br>
<button type="submit">Register</button>
</form>
<div id="message"></div>
<!-- Let the students view who all opted for particular course by selecting the course -->
<form id="redirectForm" method="get" onsubmit="redirectToRegisteredStudents(event)">
<label for="course_title">Enter Course Title:</label>
<input type="text" id="course_title" name="course_title" required>
<button type="submit">Go</button>
</form>
<script>
function redirectToRegisteredStudents(event) {
event.preventDefault(); // Prevent the default form submission
var courseTitle = document.getElementById('course_title').value;
if (courseTitle) {
// Encode the course title to be URL safe
var encodedCourseTitle = encodeURIComponent(courseTitle);
// Redirect to the desired URL
window.location.href = '/registered/' + encodedCourseTitle + '/';
}
}
$(document).ready(function(){
$('#registrationForm').on('submit', function(event){
event.preventDefault();
$.ajax({
type: 'POST',
url: '{% url "register" %}',
data: {
name: $('#name').val(),
email: $('#email').val(),
course: $('#course').val(),
csrfmiddlewaretoken: '{{ csrf_token }}'
},
success: function(response){
$('#message').html('<p>' + response.message + '</p>');
$('#registrationForm')[0].reset();
},
error: function(response){
$('#message').html('<p>An error occurred</p>');
}
});
});
});
</script>
</body>
</html>
from django.shortcuts import render, redirect, HttpResponse, get_object_or_404
from django.contrib import messages
from django.http import JsonResponse
from .models import Student, Course
def test(request):
return HttpResponse("this is a test")
def register(request):
if request.method == 'POST':
student_name = request.POST['name']
student_email = request.POST['email']
course_id = request.POST['course']
course = Course.objects.get(pk=course_id)
student, created = Student.objects.get_or_create(name=student_name, email=student_email)
course.students.add(student)
return JsonResponse({'message': 'Student successfully added to the course.'})
else:
courses = Course.objects.all()
return render(request, 'register.html', {'courses': courses})
def registered(request, course_title):
course = get_object_or_404(Course, title=course_title)
students = course.students.all()
return render(request, 'registered.html', {'course': course, 'students': students})