Как получить значение из цикла jinja for в django
У меня есть форма, которая содержит таблицу, где перечислены студенты из базы данных. Я хочу получить значение каждого студента из таблицы при нажатии на кнопку отправки, а также статус, если они присутствуют или нет.
Html код:
<div class="container">
<form method="POST" action="takeattendance">
{% csrf_token %}
<div class="form-group">
<h4 style="color:white;"> Select Subject For Attendance</h4>
<select class="btn btn-success" name="subject">
<option selected disabled="true">Subject</option>
{% for sub in subjects%}
<option value="{{ sub.id }}">{{sub.subject_name}}</option>
{%endfor%}
</select>
</div>
<table class="table" >
<thead>
<tr>
<th scope="col" style="color:white;"><b>Email</b></th>
<th scope="col" style="color:white;"><b>Roll No.</b></th>
<th scope="col" style="color:white;"><b>First Name</b></th>
<th scope="col" style="color:white;"><b>Last Name</b></th>
<th scope="col" style="color:white;"><b>Year</b></th>
<th scope="col" style="color:white;"><b>Division</b></th>
<th scope="col" style="color:white;"><b>Batch</b></th>
<th scope="col" style="color:white;"><b>Attendance</b></th>
</tr>
</thead>
<tbody>
{%for student in students%}
{%if user.staff.class_coordinator_of == student.division and user.staff.teacher_of_year == student.year%}
<tr>
<td style="color:white;" name="student_name" value="{{student.id}}">{{student.user}}</td>
<td style="color:white;">{{student.roll_no}}</td>
<td style="color:white;">{{student.user.first_name}}</td>
<td style="color:white;">{{student.user.last_name}}</td>
<td style="color:white;">{{student.year}}</td>
<td style="color:white;">{{student.division}}</td>
<td style="color:white;">{{student.batch}}</td>
<td>
<div class="form-group">
<select class="btn btn-success" name="status" id="status">
<option selected value="Present">Present</option>
<option value="Absent">Absent</option>
</select>
</div>
</td>
</tr>
{% endif %}
{%endfor%}
</tbody>
</table>
<div class="form-group">
<button type="submit" class="btn btn-info btn-lg ">Add</button>
</div>
</form>
</div>
views.py:
def staffattendance(request):
if request.user.is_authenticated and request.user.user_type==3:
subjects = Subject.objects.all()
students=Student.objects.all()
return render (request, 'ms/staff/attendance.html',{'subjects':subjects, 'students':students})
else:
return render(request,'ms/login/login.html')
def takeattendance(request):
if request.method == "POST":
subject = Subject.objects.get(id=request.POST['subject'])
student = User.objects.get(id=request.POST['student_name'])
status = request.POST['status']
attendance = Attendance(subject=subject, student=student, status=status)
attendance.save();
if request.user.is_authenticated and request.user.user_type==2:
return render(request,'ms/hod/Attendance.html')
if request.user.is_authenticated and request.user.user_type==3:
return render(request,'ms/staff/Attendance.html')
else:
return HttpResponse("Failed")
else:
return HttpResponse("Failed")
Когда я использую этот код, он выдает ошибку:
MultiValueDictKeyError for 'student_name'
Пожалуйста, помогите мне с этим.