Currently, the student information list is being generated. An option has been added to select a checkbox and click the "Add Teacher" button to include the name of the currently logged-in user in the teacher column. There is a need to apply an event to all selected rows by passing multiple values as parameters when multiple checkboxes are selected. I have searched for a solution but haven't found one yet, so any assistance would be greatly appreciated.
urls.py
path('student/add_teacher/<int:id>/', views.add_teacher)
views.py
def add_teacher(request, id):
student = Student.objects.get(pk=id)
student.teacher = request.user.name
student.save()
return HttpResponseRedirect(f'/student/')
student_list.html
<table id="student-list" class="maintable">
<thead>
<tr>
<th>Name</th>
<th>Age</th>
<th>Register date</th>
<th>Select</th>
</tr>
</thead>
<tbody>
{% for student in student %}
<tr class="text-black tr-hover table-link text-center student" student-id="{{ student.id }}">
<td>{{ student.name }}</td>
<td>{{ student.age }}</td>
<td>{{ student.register_date }}</td>
<td><input type="checkbox"></td>
</tr>
{% endfor %}
</tbody>
</table>
<button type="button" class="btn btn-secondary addteacher">Update Teacher</button>
student_function.js
$(function () {
$('button.addteacher').click(function (e) {
var elem = $(".maintable input:checked").parents("tr");
var studentID = elem.attr('student-id');
var updateTeacher = confirm("Are you sure you want to update?");
if (updateTeacher) {
window.location.href = 'student/add_teacher/' + studentID + '/';
}
});
});