You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django考勤系统开发求助:获取缺勤学生ID及批量更新数据

Django考勤系统提交问题解决方案

问题描述

我是开发新手,目前正在开发Django考勤系统。现有HTML页面展示学生列表,默认所有学生状态为Present(出勤),需求为:标记部分学生为**Absent(缺勤)**后,提交表单时未标记的学生保存为出勤,标记的保存为缺勤。但我无法正确获取每个学生对应的出勤状态,现有HTML及views.py代码如下。

现有代码

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

问题根源

  • 所有学生的状态选择框使用了相同的name="status",隐藏域也用了相同的name="student_name",表单提交后只会保留最后一个学生的ID和状态,无法批量处理所有学生。
  • 视图函数只处理单个学生的考勤,没有遍历所有提交的学生数据。

解决方案

1. 修改HTML代码

将每个学生的状态选择框的name设置为包含学生ID的唯一值,比如status_{{student.id}},同时可以移除重复的student_name隐藏域:

<!-- 替换原有的select部分 -->
<select class="btn btn-success" name="status_{{student.id}}">
    <option selected value="Present">Present</option>
    <option value="Absent">Absent</option>
</select>

2. 修改Views.py代码

遍历表单提交的所有数据,筛选出带学生ID的状态字段,批量创建考勤记录:

def takeattendance(request):
    if request.method == "POST":
        # 获取选中的科目
        subject_id = request.POST.get('subject')
        if not subject_id:
            return HttpResponse("请选择科目")
        subject = Subject.objects.get(id=subject_id)
        
        # 遍历所有提交的状态数据,处理每个学生的考勤
        for key, status in request.POST.items():
            if key.startswith('status_'):
                # 从字段名中提取学生ID
                student_id = key.split('_')[1]
                try:
                    student = Student.objects.get(id=student_id)
                    # 验证教师是否有权限处理该学生的考勤
                    if request.user.staff.class_coordinator_of == student.division and request.user.staff.teacher_of_year == student.year:
                        # 创建考勤记录
                        Attendance.objects.create(subject=subject, student=student, status=status)
                except Student.DoesNotExist:
                    # 跳过不存在的学生
                    continue
        
        # 根据用户类型返回对应页面
        if request.user.is_authenticated:
            if request.user.user_type == 2:
                return render(request,'ms/hod/Attendance.html')
            elif request.user.user_type == 3:
                return render(request,'ms/staff/Attendance.html')
        return HttpResponse("Failed")
    else:    
        return HttpResponse("Failed")

说明

  • 修改后的HTML每个学生的状态字段都有唯一标识,提交后能区分每个学生的出勤状态。
  • 视图函数通过遍历表单数据,批量处理所有符合条件的学生考勤,默认状态Present会被正常提交,无需额外处理。

内容的提问来源于stack exchange,提问作者Ajay

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 12:54:25