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

如何在Flask问卷页面实现学生与对应班主任的匹配校验?

验证学生所选班主任是否为自身班级班主任的解决方案

实现步骤

基于你的数据库关联规则(student.class_id = teacher.id),需要在POST请求处理逻辑中新增校验,具体如下:

1. 修改app.py的POST处理代码

在现有"是否已提交问卷"的校验后,加入班主任匹配校验:

@app.route("/questionnaire/page", methods = ["GET", "POST"])
def questionnaire():
    cursor = get_db().cursor()
    if request.method == "POST":
        try:
            form = request.form
            student_id = int(form.get("student_id"))
            # 获取用户选择的班主任ID(需和前端表单字段名对应,这里假设是teacher_id)
            selected_teacher_id = int(form.get("teacher_id"))

            cursor.execute("BEGIN")
            # 一次性查询学生ID和对应的班级(班主任)ID
            cursor.execute("""
                SELECT id, class_id
                FROM student
                WHERE id = ? 
                LIMIT 1
            """, [student_id])
            student_data = cursor.fetchone()

            if not student_data:
                # 学生不存在,返回400
                cursor.execute("COMMIT")
                abort(400)
            if student_data:
                # 已提交过问卷,跳转对应页面
                cursor.execute("COMMIT")
                return render_template("questionnaire_already_answered.html")
            
            # 校验所选班主任是否匹配
            student_class_id = student_data[1]
            if selected_teacher_id != student_class_id:
                cursor.execute("COMMIT")
                # 重新渲染问卷页面并传递错误信息
                students = cursor.execute("SELECT * FROM student").fetchall()
                teachers = cursor.execute("SELECT * FROM teacher").fetchall()
                courses = cursor.execute("SELECT * FROM course").fetchall()
                return render_template(
                    "questionnaire.html",
                    students=students,
                    teachers=teachers,
                    courses=courses,
                    error_msg="请选择您所在班级的专属班主任"
                )
            
            # 此处添加问卷数据入库的逻辑(如果需要)
            # ...

            cursor.execute("COMMIT")
            # 提交成功后跳转感谢页面
            return render_template("questionnaire_success.html")
        except (TypeError, ValueError, sqlite3.DatabaseError):
            abort(400)
    
    # GET请求逻辑保持不变
    cursor.execute("SELECT * FROM student")
    students = cursor.fetchall()
    teachers = cursor.execute("SELECT * FROM teacher").fetchall()
    courses = cursor.execute("SELECT * FROM course").fetchall()
    return render_template("questionnaire.html", students=students, teachers=teachers, courses=courses)

2. 前端页面适配

在questionnaire.html中添加错误信息展示区域,用于接收后端传递的error_msg:

{% if error_msg %}
    <div style="color: red; margin-bottom: 1rem;">{{ error_msg }}</div>
{% endif %}

关键注意事项

  • 确保前端表单中班主任选择框的name属性为teacher_id,如果实际字段名不同,需修改代码中form.get("teacher_id")的参数
  • 原代码中查询学生的逻辑被优化为一次获取id和class_id,减少数据库查询次数,提升效率

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.24 20:50:18