def edit_student(request): if request.method == "GET": id = request.GET.get("id") obj = SQLHelper() class_list = obj.get_list("select id,name from class", []) student = obj.get_one( "select student.id,student.sname,student.cid,class.name from student left join class on student.cid = class.id where student.id = %s", id) obj.close() return render(request, "edit_student.html", { "student": student, "class_list": class_list }) else: id = request.POST.get("id") sname = request.POST.get("sname") cid = request.POST.get("class_id") obj = SQLHelper() obj.modify("update student set sname = %s,cid = %s where id = %s", [ sname, cid, id, ]) obj.close() return redirect("/students/")
def edit_class(request): if request.method == "GET": id = request.GET.get("id") obj = SQLHelper() result = obj.get_one("select id,name from class where id = %s", id) obj.close() return render(request, 'edit_class.html', {"result": result}) else: id = request.GET.get("id") # id = request.POST.get("id") name = request.POST.get("name") obj = SQLHelper() obj.modify("update class set name = %s where id = %s", [ name, id, ]) obj.close() return redirect("/class/")
def editTearcher(request): if request.method == 'GET': tid = request.GET.get('tid') obj = SQLHelper() tearcher_info = obj.get_one( 'select tid,tname from tearcher where tid = %s', [tid]) tearcher_class_list = obj.get_list( 'select cid from tearch2class where tearch2class.tid = %s', [ tid, ]) class_list = obj.get_list('select id,name from class') obj.close() temp = [] for i in tearcher_class_list: temp.append(i['cid']) return render( request, 'edit_tearcher.html', { 'info': tearcher_info, 'tearcher_class_list': temp, 'class_list': class_list }) else: tid = request.GET.get('tid') class_ids = request.POST.getlist('class_ids') tname = request.POST.get('tname') #更新老师表 obj = SQLHelper() obj.modify('update tearcher set tname=%s where tid = %s', [tname, tid]) #更细老师班级关系表 # 方案一:先删除老师和班级的对应关系删除,之后再做添加 obj.modify('delete from tearch2class where tid = %s', [tid]) sql_args = [] for cid in class_ids: temp = (tid, cid) sql_args.append(temp) obj.multiple_modify( 'insert into tearch2class(tid, cid) values (%s,%s)', sql_args) obj.close() # 方案二:把新的班级数据和老的数据做对比进行删除和增加 return redirect('/tearchers/')