Могу ли я получить код для получения id с помощью javascript и ajax

Я новичок в разработке и не имею большого представления об этом, пожалуйста, помогите мне, я не могу получить код для получения идентификатора отсутствующего студента с помощью javascript, пожалуйста, помогите мне с этим.

Это мой html код, где отображается список студентов, все студенты по умолчанию присутствуют, и если я отмечаю какого-то студента как отсутствующего, все остальные студенты должны быть сохранены как присутствующие и другие отмеченные как отсутствующие, которые были выбраны

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;"><input type="hidden" 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 takeattendance(request):
if request.method == "POST":
    subject = Subject.objects.get(id=request.POST['subject'])
    student = Student.objects.get(id=request.POST['student_name'])
    status = request.POST['status']
    print(subject)
    print(student)
    print(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") 

Пожалуйста, помогите мне с этим

Я сделал некоторые изменения в вашем views.py

def takeattendance(request):
    if request.method == "POST":
        subject = Subject.objects.get(id=request.POST['subject'])
        student_ids = request.POST.getlist('student_name')
        status_list = request.POST.getlist('status')

        attendance_objs = []
        for sid, status in zip(student_ids, status_list):
            attendance_objs.append(
                Attendance(
                  subject=subject,
                  student=Student.objects.get(id=sid),
                  status=status
                )
            )
        
        Attendance.objects.bulk_create(
                attendance_objs
        )

        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")

Я взял статус как список значений и использовал zip() для объединения двух списков student_ids, status_list и затем взял sid, status из этого.

Вернуться на верх