【学员管理系统】0x03 老师信息管理功能
【学员管理系统】0x03 老师信息管理功能
老师信息管理相比于学生信息管理又多了一点,因为我们的数据结构中老师表和班级表是通过teacher2class表进行多对多关联的。
写在前面
项目详细需求参见:Django项目之【学员管理系统】
展示老师信息
后端部分
注意连表查询操作
def teacher_list(request): # 连接数据库 conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="root1234", db="mysite", charset="utf8") cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) sql = "select teacher.id, teacher.name, class.name as cname from teacher LEFT JOIN teacher2class on teacher.id = teacher2class.teacher_id LEFT JOIN class ON teacher2class.class_id = class.id;" cursor.execute(sql) teacher_list_o = cursor.fetchall() # 将查询到的数据类型转换一下 teacher_list = magic(teacher_list_o) return render(request, "teacher_list.html", {"teacher_list": teacher_list})
前端部分
将老师关联的班级信息也展示出来。
<table class="table table-bordered table-striped"> <thead> <tr> <th>#</th> <th>老师</th> <th>班级</th> <th>操作</th> </tr> </thead> <tbody> {% for teacher in teacher_list %} <tr> <th scope="row">{{ teacher.id }}</th> <td>{{ teacher.name }}</td> <td> {% for class in teacher.class_list %} <span>{{ class }}</span>| {% endfor %} </td> <td class="text-center"> <a type="button" class="btn btn-sm btn-success m-edit" aria-label="Left Align"> <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>编辑 </a> | <a href="/edit_teacher/?teacher_id={{ teacher.id }}/" type="button" class="btn btn-sm btn-success" aria-label="Left Align"> <span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>新页面编辑 </a> | <a href="/delete_teacher/?teacher_id={{ teacher.id }}" type="button" class="btn btn-sm btn-danger" aria-label="Left Align"> <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除 </a> </td> </tr> {% empty %} <tr> <td colspan="3" style="text-align: center">空空如也~</td></tr> {% endfor %} </tbody> </table>
删除老师信息
后端部分
后端从请求的URL中提取到要删除的老师的ID。
def delete_teacher(request): # 从GET请求的URL中取到要删除的老师ID teacher_id = request.GET.get("student_id") # 连接数据库 conn = pymysql.connect(host="127.0.0.1", port=3306, user="root", passwd="root1234", db="mysite", charset="utf8") cursor = conn.cursor(cursor=pymysql.cursors.DictCursor) # 删除指定的老师 sql = "delete from teacher WHERE id=%s;" # 执行SQL语句 cursor.execute(sql, [teacher_id, ]) conn.commit() conn.close() # 删除成功,跳转到老师列表页 return redirect("/teacher_list/")
前端部分
点击页面上对应老师记录的删除按钮,向后端发送GET请求,将要删除的老师的ID传递给后端。
删除按钮部分HTML代码:
<a href="/delete_teacher/?teacher_id={{ class.id }}" type="button" class="btn btn-sm btn-danger" aria-label="Left Align"> <span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除 </a>
新增老师信息
后端部分
这里我们对我们的数据库连接部分的代码做了优化。
from tools.sql_master import create, SQLManager def add_teacher(request): if request.method == "POST": class_list = request.POST.getlist("class_id") teacher_name = request.POST.get("teacher_name") # 创建老师 teacher_id = create("insert into teacher(name) VALUES (%s)", [teacher_name, ]) # 更新teacher2class表 # 多次链接,多次提交 # for i in class_list: # modify("insert into teacher2class(teacher_id, class_id) VALUES (%s, %s)", [teacher_id, i]) # # # 一次链接,多次提交 # db = SQLManager() # for i in class_list: # db.moddify("insert into teacher2class(teacher_id, class_id) VALUES (%s, %s)", [teacher_id, i]) # db.close() # # 一次链接,一次提交 data_list = [] for i in class_list: tmp = [teacher_id, i] data_list.append(tmp) db = SQLManager() db.multi_modify("insert into teacher2class(teacher_id, class_id) VALUES (%s, %s)", data_list) db.close() return redirect("/teacher_list/") else: class_list = get_list("select id, name from class") return render(request, "add_teacher.html", {"class_list": class_list})
前端部分
因为我们添加老师信息的时候,需要给老师指定关联的班级信息,所以在页面上需要将现有的班级信息展示出来。
新增老师信息时,输入老师的姓名然后选择关联的班级信息即可。
<form class="form-horizontal" action="/add_teacher/" method="post"> <div class="form-group"> <label for="inputclassname" class="col-sm-2 control-label">老师姓名</label> <div class="col-sm-10"> <input type="text" class="form-control" name="teacher_name" id="inputclassname" placeholder="老师姓名"> </div> </div> <div class="form-group"> <label for="selectclass" class="col-sm-2 control-label">班级</label> <div class="col-sm-10"> <select class="form-control" name="class_id" multiple> {% for class in class_list %} <option value="{{ class.id }}">{{ class.name }}</option> {% endfor %} </select> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">提交</button> </div> </div> </form>
编辑老师信息
后端部分
def edit_teacher(request): if request.method == "POST": teacher_id = request.POST.get("teacher_id") class_ids = request.POST.getlist("class_id") # 更新 db = SQLManager() teacher_class_ids = db.get_list("select class_id from teacher2class WHERE teacher_id=%s", [teacher_id, ]) old_class_ids = [i["class_id"] for i in teacher_class_ids] # 粗暴更新 del_id_list = [] add_id_list = [] for i in old_class_ids: del_id_list.append((teacher_id, i)) for j in class_ids: add_id_list.append((teacher_id, j)) db.multi_modify("DELETE from teacher2class WHERE teacher_id=%s AND class_id=%s", del_id_list) db.multi_modify("insert into teacher2class(teacher_id, class_id) VALUES (%s, %s)", add_id_list) db.close() return redirect("/teacher_list") else: teacher_id = request.GET.get("teacher_id") with SQLManager() as db: class_list = db.get_list("select id, name from class") teacher_info = db.get_list("SELECT teacher.id, teacher.name, teacher2class.class_id FROM teacher LEFT JOIN teacher2class ON teacher.id = teacher2class.teacher_id WHERE teacher.id=%s;", [teacher_id]) ret = teacher_info[0] ret["class_ids"] = [ret["class_id"], ] for i in teacher_info[1:]: ret["class_ids"].append(i["class_id"]) return render(request, "edit_teacher.html", {"class_list": class_list, "teacher": ret})
前端部分
<form class="form-horizontal" action="/edit_teacher/" method="post"> <input type="text" name="teacher_id" value="{{ teacher.id }}" style="display: none"> <div class="form-group"> <label for="inputclassname" class="col-sm-2 control-label">学生姓名</label> <div class="col-sm-10"> <input type="text" class="form-control" name="teacher_name" id="inputteachername" placeholder="老师姓名" value="{{ teacher.name }}"> </div> <span id="helpBlock2" class="help-block">{{ error }}</span> </div> <div class="form-group"> <label for="selectclass" class="col-sm-2 control-label">班级</label> <div class="col-sm-10"> <select class="form-control" name="class_id" multiple> {% for class in class_list %} {% if class.id in teacher.class_ids %} <option selected value="{{ class.id }}">{{ class.name }}</option> {% else %} <option value="{{ class.id }}">{{ class.name }}</option> {% endif %} {% endfor %} </select> </div> </div> <div class="form-group"> <div class="col-sm-offset-2 col-sm-10"> <button type="submit" class="btn btn-default">提交</button> </div> </div> </form>
可利用ORM实现模态框版增删改操作