1. 一对一 班级 模态增加 编辑
def classes(request): data = sqlheper.get_list("select cid,title from class",[]) return render(request, "classes.html", {"data": data}) def motai_add_class(request): ret = {'status': True, 'message': None} title = request.POST.get('title') try: nid = request.POST.get('nid') content = request.POST.get('content') sqlheper.motify_sql('insert into class(title) values(%s)',[title,]) except Exception as e: ret['status'] = False ret['message'] = "处理异常" return HttpResponse(json.dumps(ret)) def modal_edit_class(request): print(request.POST) ret = {'status': True, 'message':None} try: id = request.POST.get('id') title = request.POST.get('title') sqlheper.motify_sql('update class set title=%s where cid=%s',[title,id,]) except Exception as e: ret['status'] = False ret['message'] = "处理异常" return HttpResponse(json.dumps(ret)) classes.html
2.一对多 学生班级 模态增加 编辑
1 DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Titletitle> 6 <style> 7 .hide{ 8 display: none; 9 } 10 .shadow{ 11 position: fixed; 12 top: 0; 13 bottom: 0; 14 left: 0; 15 right: 0; 16 background-color: black; 17 z-index: 999; 18 opacity: 0.4; 19 } 20 .Modal{ 21 position: fixed; 22 top: 50%; 23 left: 50%; 24 width: 400px; 25 height: 300px; 26 margin-left: -200px; 27 margin-top: -150px; 28 z-index: 1000; 29 background-color: white; 30 } 31 style> 32 head> 33 <body> 34 35 <h1>班级学员h1> 36 37 <div> 38 <a id="addStudent">模态框增加a> 39 div> 40 41 <table border="1px"> 42 <thead> 43 <tr> 44 <td>学员名称td> 45 <td>学生名称td> 46 <td>班级名称td> 47 <td>模态操作td> 48 tr> 49 thead> 50 <tbody> 51 {% for row in student_list %} 52 <tr> 53 <td>{{ row.sid }}td> 54 <td>{{ row.name }}td> 55 <td clsId="{{ row.class_id }}">{{ row.title }}td> 56 <td> 57 <a class="btn-edit">编辑a> 58 <a >删除a> 59 td> 60 tr> 61 {% endfor %} 62 tbody> 63 table> 64 65 <div id="shadow" class="shadow hide">div> 66 67 68 {#增加#} 69 <div id="addModal" class="Modal hide"> 70 71 72 <p>学生名称: 73 <input id="add_name" type="text" name="add_name"> 74 p> 75 <p>学生性别: 76 <input id="add_sex" type="text" name="add_sex"> 77 p> 78 <p>班级名称: 79 <select id="add_classId" name="add_classId"> 80 {% for row in class_list %} 81 <option value="{{ row.cid }}">{{ row.title }}option> 82 {% endfor %} 83 select> 84 p> 85 86 <input id="btnAdd" type="button" value="提交"><span id="addError">span> 87 <input id="btnCancle" type="button" value="取消"> 88 89 div> 90 91 {#编辑#} 92 <div id="editModal" class="Modal hide"> 93 <h3>编辑学生信息h3> 94 <p> 95 姓名:<input id="editName" type="text" name="name" placeholder="姓名" /> 96 <input type="text" id="editId" style="display: none" /> 97 p> 98 <p> 99 班级: 100 <select id="editClassId" name="classId"> 101 {% for row in class_list %} 102 <option value="{{ row.cid }}">{{ row.title }}option> 103 {% endfor %} 104 select> 105 p> 106 <input id="btnEdit" type="button" value="更新" /> 107 <span id="editError" style="color: red;">span> 108 <input id="btnCancle" type="button" value="取消" /> 109 div> 110 111 <script src="/static/jquery-3.2.1.js">script> 112 113 <script> 114 115 $(function () { 116 117 {# 增加#} 118 119 $("#addStudent").click(function () { 120 $("#shadow,#addModal").removeClass("hide"); 121 }); 122 123 $("#btnCancle").click(function () { 124 $("#shadow,#addModal").addClass("hide"); 125 $("#editModal").addClass("hide"); 126 }); 127 128 $("#btnAdd").click(function () { 129 var add_name=$("#add_name").val(); 130 var add_age=$("#add_sex").val(); 131 var add_classId=$("#add_classId").val(); 132 $.ajax({ 133 url:"/motai_add_student/", 134 type:"POST", 135 data:{"add_name":add_name,"add_age":add_age,"add_classId":add_classId}, 136 success:function (arg) { 137 arg = JSON.parse(arg); 138 if (arg.status){ 139 location.reload(); 140 }else { 141 $("#addError").text(arg.message); 142 } 143 } 144 }) 145 146 }); 147 148 149 {# 编辑 #} 150 $('.btn-edit').click(function(){ 151 $('#shadow,#editModal').removeClass('hide'); 152 153 var tds = $(this).parent().prevAll(); 154 var studentId = $(tds[2]).text(); 155 var studentName = $(tds[1]).text(); 156 var classId = $(tds[0]).attr('clsid'); 157 158 console.log(studentId,studentName,classId); 159 160 $('#editId').val(studentId); 161 $('#editName').val(studentName); 162 $('#editClassId').val(classId); 163 }); 164 165 $('#btnEdit').click(function(){ 166 $.ajax({ 167 url:'/motai_edit_student/', 168 type: 'POST', 169 data: {'sid': $('#editId').val(), 'name':$('#editName').val(),'class_id': $('#editClassId').val()}, 170 dataType: 'JSON', //JSON.parse(arg) 171 success:function(arg){ 172 if(arg.status){ 173 location.reload(); 174 }else{ 175 $('#editError').text(arg.message); 176 } 177 } 178 }) 179 }); 180 }) 181 182 script> 183 184 body> 185 html> 186 187 student.html
1 def student(request): 2 student_list = sqlheper.get_list("select student.sid,student.name,student.class_id,class.title from student left join class on student.class_id=class.cid",[]) 3 4 class_list = sqlheper.get_list("select cid,title from class",[]) 5 6 return render(request, "student.html", {"student_list":student_list, "class_list":class_list}) 7 8 9 def motai_add_student(request): 10 print(request.POST) 11 ret = {"status":True,"message":None} 12 try: 13 name = request.POST.get("add_name") 14 age = request.POST.get("add_age") 15 classId = request.POST.get("add_classId") 16 17 sqlheper.motify_sql("insert into student(name,age,class_id) values(%s,%s,%s)",[name,age,classId,]) 18 except Exception as e: 19 ret["status"] = False 20 ret["message"] = str(e) 21 return HttpResponse(json.dumps(ret)) 22 23 def motai_edit_student(request): 24 ret = {'status': True,'message': None} 25 try: 26 27 print(request.POST) 28 sid = request.POST.get('sid') 29 name = request.POST.get('name') 30 class_id = request.POST.get('class_id') 31 sqlheper.motify_sql('update student set name=%s,class_id=%s where sid=%s',[name,class_id,sid,]) 32 except Exception as e: 33 ret['status'] = False 34 ret['message'] = str(e) 35 return HttpResponse(json.dumps(ret)) 36 37 Views
3.多对多 老师 班级 模态增加 编辑
1 def teacher(request): 2 3 teacher_list=sqlheper.get_list(""" 4 select teacher.tid as tid,teacher.name,class.title from teacher 5 left join teacher_class on teacher_class.teacher_id=teacher.tid 6 left join class on class.cid=teacher_class.class_id""",[]) 7 # print(teacher_list) 8 result = {} 9 for row in teacher_list: 10 tid = row["tid"] 11 if tid in result: 12 result[tid]["titles"].append(row["title"]) 13 else: 14 result[tid] = {"tid":row["tid"],"name":row["name"],"titles":[row["title"],]} 15 16 class_list = sqlheper.get_list("select cid,title from class",[]) 17 18 return render(request, "teacher.html_模态增加 老师班级", {"teacher_list":result.values(), "class_list":class_list}) 19 20 ###模态增加 21 def new_teacher(request): 22 print(request.POST) 23 ret = {'status': True, 'message': None} 24 try: 25 class_list=request.POST.getlist("class_list[]") 26 tname=request.POST.get("tname") 27 print(class_list) 28 print(tname) 29 teacher_id=sqlheper.get_IncrementId("insert into teacher(name) values(%s)",[tname,]) 30 for item in class_list: 31 sqlheper.motify_sql("insert into teacher_class(teacher_id,class_id) values(%s,%s)",[teacher_id,item,]) 32 except Exception as e: 33 ret['status'] = False 34 ret['message'] = str(e) 35 return HttpResponse(json.dumps(ret)) 36 37 views
1 DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Titletitle> 6 <style> 7 .hide{ 8 display: none; 9 } 10 .shadow{ 11 position: fixed; 12 top: 0; 13 bottom: 0; 14 left: 0; 15 right: 0; 16 background-color: black; 17 z-index: 999; 18 opacity: 0.4; 19 } 20 .Modal{ 21 position: fixed; 22 top: 50%; 23 left: 50%; 24 width: 400px; 25 height: 300px; 26 margin-left: -200px; 27 margin-top: -150px; 28 z-index: 1000; 29 background-color: white; 30 } 31 style> 32 head> 33 <body> 34 35 <h1>老师 班级管理h1> 36 37 <div> 38 39 <a id="addModal">模态框增加a> 40 div> 41 42 <table border="solid" > 43 <thead> 44 <tr> 45 <td>IDtd> 46 <td>老师名称td> 47 <td>班级名称td> 48 <td>操作td> 49 tr> 50 thead> 51 <tbody> 52 {% for row in teacher_list %} 53 <tr> 54 <td>{{ row.tid }}td> 55 <td>{{ row.name }}td> 56 <td> 57 {% for item in row.titles %} 58 {{ item }} 59 {% endfor %} 60 td> 61 <td> 62 <a class="editModal">编辑a> 63 <a class="delModal">删除a> 64 td> 65 tr> 66 {% endfor %} 67 tbody> 68 69 table> 70 71 <div id="shadow" class="shadow hide">div> 72 73 <div id="add_tea_cls" class="Modal hide"> 74 75 <p>老师名称: 76 <input id="add_name" type="text" name="add_name"> 77 p> 78 79 <p>班级名称: 80 <select id="add_classId" name="add_classId" multiple> 81 {% for row in class_list %} 82 <option value="{{ row.cid }}">{{ row.title }}option> 83 {% endfor %} 84 select> 85 p> 86 <input id="btnAdd" type="button" value="提交"><span id="addError">span> 87 <input id="btnCancle" type="button" value="取消"> 88 89 div> 90 91 <div id="edit_tea_cls" class="Modal hide"> 92 93 <p>老师名称: 94 <input id="add_name" type="text" name="add_name"> 95 p> 96 97 <p>班级名称: 98 <select id="add_classId" name="add_classId" multiple> 99 {% for row in class_list %} 100 <option value="{{ row.cid }}">{{ row.title }}option> 101 {% endfor %} 102 select> 103 p> 104 <input id="btnEdit" type="button" value="提交"><span id="addError">span> 105 <input id="cacleEdit" type="button" value="取消"> 106 107 div> 108 109 110 <script src="/static/jquery-3.2.1.js">script> 111 112 <script> 113 114 $(function () { 115 116 117 {# 增加#} 118 $("#addModal").click(function () { 119 $("#shadow,#add_tea_cls").removeClass("hide"); 120 }); 121 122 $("#btnCancle").click(function () { 123 $("#shadow,#add_tea_cls").addClass("hide"); 124 }); 125 126 127 $("#btnAdd").click(function () { 128 tname=$("#add_name").val(); 129 class_list=$("#add_classId").val(); 130 console.log(class_list) 131 $.ajax({ 132 url:"/new_teacher/", 133 type:"POST", 134 data:{"tname":tname,"class_list":class_list}, 135 success:function (arg) { 136 arg = JSON.parse(arg); 137 if (arg.status){ 138 location.reload(); 139 }else { 140 $("#addError").text(arg.message); 141 } 142 } 143 }) 144 }); 145 146 147 148 {# 编辑#} 149 $(".editModal").click(function () { 150 $("#shadow").removeClass("hide"); 151 $("#edit_tea_cls").removeClass("hide"); 152 }); 153 154 $("#cacleEdit").click(function () { 155 $("#shadow,#edit_tea_cls").addClass("hide"); 156 }); 157 158 159 }) 160 161 162 163 script> 164 165 166 167 body> 168 html> 169 170 teacher.html
4.多对多 老师 班级 新url 增加 编辑
1 DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Titletitle> 6 7 head> 8 <body> 9 10 <h1>老师 班级管理h1> 11 12 <div> 13 14 <a href="/add_page_teacher/">增加a> 15 div> 16 17 <table border="solid" > 18 <thead> 19 <tr> 20 <td>IDtd> 21 <td>老师名称td> 22 <td>班级名称td> 23 <td>操作td> 24 tr> 25 thead> 26 <tbody> 27 {% for row in teacher_list %} 28 <tr> 29 <td>{{ row.tid }}td> 30 <td>{{ row.name }}td> 31 <td> 32 {% for item in row.titles %} 33 {{ item }} 34 {% endfor %} 35 td> 36 <td> 37 <a href="/edit_page_teacher/?tid={{ row.tid }}">编辑a> 38 <a >删除a> 39 td> 40 tr> 41 {% endfor %} 42 tbody> 43 44 table> 45 46 47 body> 48 html> 49 50 teacher.html
1 DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Titletitle> 6 head> 7 <body> 8 9 <h1>增加老师h1> 10 11 <form action="/add_teacher/" method="POST"> 12 <p>老师名称: <input type="text" name="name">p> 13 <input type="submit"> 14 form> 15 16 body> 17 html> 18 19 add_page.teacher
1 DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Titletitle> 6 head> 7 <body> 8 9 <h3>编辑老师班级h3> 10 11 12 <form action="/edit_page_teacher/?tnid={{ tname.tid }}" method="post"> 13 14 <p>老师名称:<input type="text" name="name" value="{{ tname.name }}">p> 15 16 <p>班级名称: 17 <select name="class_ids" multiple size="7"> 18 {% for item in class_list %} 19 {% if item.cid in class_ids %} 20 <option selected value="{{ item.cid }}">{{ item.title }}option> 21 {% else %} 22 <option value="{{ item.cid }}">{{ item.title }}option> 23 {% endif %} 24 {% endfor %} 25 select> 26 27 <p><input type="submit" value="提交">p> 28 p> 29 30 form> 31 32 33 body> 34 html> 35 36 edit_teacher
1 ###网页显示 2 def teacher(request): 3 4 teacher_list=sqlheper.get_list(""" 5 select teacher.tid as tid,teacher.name,class.title from teacher 6 left join teacher_class on teacher_class.teacher_id=teacher.tid 7 left join class on class.cid=teacher_class.class_id""",[]) 8 # print(teacher_list) 9 result = {} 10 for row in teacher_list: 11 tid = row["tid"] 12 if tid in result: 13 result[tid]["titles"].append(row["title"]) 14 else: 15 result[tid] = {"tid":row["tid"],"name":row["name"],"titles":[row["title"],]} 16 17 class_list = sqlheper.get_list("select cid,title from class",[]) 18 19 return render(request, "teacher.html", {"teacher_list":result.values(), "class_list":class_list}) 20 21 22 ###网页增加 23 def add_page_teacher(request): 24 if request.method=="GET": 25 obj = sqlheper.SqlHelper() 26 class_list = obj.get_list("select cid,title from class",[]) 27 obj.close() 28 return render(request,"add_page_teacher.html",{"class_list":class_list}) 29 else: 30 31 name = request.POST.get("name") 32 obj = sqlheper.SqlHelper() 33 teacher_id = obj.get_lastrowid("insert into teacher(name) values(%s)",[name,]) 34 obj.close() 35 36 37 class_ids = request.POST.getlist("class_ids") 38 print(class_ids) 39 40 data_list = [] 41 for cls_id in class_ids: 42 temp = (teacher_id, cls_id,) 43 data_list.append(temp) 44 45 obj = sqlheper.SqlHelper() 46 obj.multiple_modify('insert into teacher_class(teacher_id,class_id) values(%s,%s)', data_list) 47 obj.close() 48 return redirect('/teacher/') 49 50 51 ###网页编辑 52 def edit_page_teacher(request): 53 if request.method=="GET": 54 teacher_id = request.GET.get("tid") 55 obj = sqlheper.SqlHelper() 56 tname = obj.get_one("select tid,name from teacher where tid=%s",[teacher_id,]) 57 58 59 class_list = obj.get_list("select cid,title from class",[]) 60 61 class_ids = obj.get_list("select class_id from teacher_class where teacher_id =%s",[teacher_id,]) 62 obj.close() 63 64 temp = [] 65 for i in class_ids: 66 temp.append(i['class_id']) 67 68 return render(request,"edit_page_teacher.html",{ 69 "tname":tname, 70 "class_list":class_list, 71 "class_ids":temp, 72 }) 73 else: 74 tid=request.GET.get("tnid") 75 name=request.POST.get("name") 76 class_ids=request.POST.getlist("class_ids") 77 78 obj = sqlheper.SqlHelper() 79 obj.modify("update teacher set name=%s where tid=%s",[name,tid,]) 80 obj.modify('delete from teacher_class where teacher_id=%s',[tid,]) 81 data_list = [] 82 for cls_id in class_ids: 83 temp = (tid,cls_id) 84 data_list.append(temp) 85 obj.multiple_modify('insert into teacher_class(teacher_id,class_id) values(%s,%s)',data_list) 86 obj.close() 87 return redirect('/teacher/') 88 89 views
5.多对多 老师 班级 模态 增加 编辑
1 DOCTYPE html> 2 <html lang="en"> 3 <head> 4 <meta charset="UTF-8"> 5 <title>Titletitle> 6 <style> 7 .hide{ 8 display: none; 9 } 10 .shadow{ 11 position: fixed; 12 top: 0; 13 bottom: 0; 14 left: 0; 15 right: 0; 16 background-color: black; 17 z-index: 999; 18 opacity: 0.4; 19 } 20 .loading{ 21 position: fixed; 22 width: 32px; 23 height: 32px; 24 left: 50%; 25 top:50%; 26 margin-left: -16px; 27 margin-top: -16px; 28 background-color: rebeccapurple; 29 background-image: url("/static/images/loading.gif") ; 30 background-size: 100%; 31 32 } 33 .Modal{ 34 position: fixed; 35 top: 50%; 36 left: 50%; 37 width: 400px; 38 height: 300px; 39 margin-left: -200px; 40 margin-top: -150px; 41 z-index: 1000; 42 background-color: white; 43 } 44 style> 45 <link rel="stylesheet" href="/static/plugins/bootstrap-3.3.7-dist/css/bootstrap.css"> 46 <link rel="stylesheet" href="/static/plugins/font-awesome-4.7.0/css/font-awesome.css"> 47 head> 48 <body> 49 50 <h1 >老师 班级管理h1> 51 52 <div class="btn btn-success"> 53 54 <a id="addModal">模态框增加a> 55 div> 56 57 <table border="solid" class="table table-striped"> 58 <thead> 59 <tr> 60 <td>IDtd> 61 <td>老师名称td> 62 <td>班级名称td> 63 <td>操作td> 64 tr> 65 thead> 66 <tbody> 67 {% for row in teacher_list %} 68 <tr> 69 <td>{{ row.tid }}td> 70 <td>{{ row.name }}td> 71 <td> 72 {% for item in row.titles %} 73 {{ item }} 74 {% endfor %} 75 td> 76 <td> 77 <a class="editModal">编辑a> 78 <a class="delModal">删除a> 79 td> 80 tr> 81 {% endfor %} 82 tbody> 83 84 table> 85 86 <div id="shadow" class="shadow hide">div> 87 88 <div id="loading" class="loading hide">div> 89 90 <div id="add_tea_cls" class="Modal hide"> 91 92 <p>老师名称: 93 <input id="add_name" type="text" name="add_name"> 94 p> 95 96 <p>班级名称: 97 <select id="add_classId" name="add_classId" multiple size="10"> 98 99 select> 100 p> 101 <input id="btnAdd" type="button" value="提交"><span id="addError">span> 102 <input id="btnCancle" type="button" value="取消"> 103 104 div> 105 106 <div id="edit_tea_cls" class="Modal hide"> 107 <input type="text" id="hide_id" style="display: none"> 108 109 <p>老师名称: 110 <input id="edit_name" type="text" name="add_name"> 111 p> 112 113 <p>班级名称: 114 <select id="edit_classId" name="edit_classId" multiple size="10"> 115 116 select> 117 p> 118 <input id="btnEdit" type="button" value="提交"><span id="addError">span> 119 <input id="cacleEdit" type="button" value="取消"> 120 121 div> 122 123 124 <script src="/static/jquery-3.2.1.js">script> 125 126 <script> 127 128 $(function () { 129 130 131 {# 增加#} 132 $("#addModal").click(function () { 133 $("#shadow,#loading").removeClass("hide"); 134 $.ajax({ 135 url:"/get_all_class/", 136 type:"GET", 137 dataType:"JSON", 138 success:function (arg) { 139 $.each(arg,function (i,row) { 140 var tag = $(""); /*var tag = document.createElement('option');*/ 141 tag.text(row.title); /*tag.innerHTML = row.title;*/ 142 tag.prop("value",row.cid); 143 $("#add_classId").append(tag); /*tag.setAttribute('value',row.id);*/ 144 }); 145 $('#loading').addClass('hide'); 146 $('#add_tea_cls').removeClass('hide'); 147 } 148 }) 149 }); 150 151 $("#btnCancle").click(function () { 152 $("#shadow,#add_tea_cls").addClass("hide"); 153 }); 154 155 $("#btnAdd").click(function () { 156 var tname=$("#add_name").val(); 157 var class_list=$("#add_classId").val(); 158 console.log(class_list); 159 $.ajax({ 160 url:"/new_teacher/", 161 type:"POST", 162 data:{"tname":tname,"class_list":class_list}, 163 dataType:"JSON", 164 traditional: true, // 如果提交的数据的值有列表,则需要添加此属性 165 success:function (arg) { 166 167 if (arg.status){ 168 location.reload(); 169 }else { 170 alert(arg.message); 171 } 172 } 173 }) 174 }); 175 176 177 178 {# 编辑#} 179 $(".editModal").click(function () { 180 $("#shadow,#loading").removeClass("hide"); 181 var ids=$(this).parent().prevAll()[2]; 182 var id=$(ids).text(); 183 $("#hide_id").val(id); 184 $.ajax({ 185 url: "/edit_tea_cls/", 186 type: "POST", 187 dataType: "JSON", 188 data:{"id":id}, 189 success: function (arg) { 190 191 class_list = arg[0]; 192 teacher_info = arg[1]; 193 class_lds = arg[2]; 194 console.log(class_lds); 195 $("#edit_classId").empty(); 196 $.each(class_list, function (i, row) { 197 var tag = $(""); 198 tag.text(row.title); 199 tag.prop("value", row.cid); 200 201 if(class_lds.indexOf(row.cid) == -1){ 202 $("#edit_classId").append(tag); 203 }else { 204 tag.prop("selected","selected"); 205 $("#edit_classId").append(tag); 206 } 207 }); 208 $("#edit_name").val(teacher_info["name"]); 209 210 211 212 213 214 215 216 $('#loading').addClass('hide'); 217 $('#edit_tea_cls').removeClass('hide'); 218 } 219 }); 220 221 $("#cacleEdit").click(function () { 222 $("#shadow,#edit_tea_cls").addClass("hide"); 223 }); 224 }) 225 226 {# 编辑提交#} 227 $("#btnEdit").click(function () { 228 var tid= $("#hide_id").val(); 229 var name = $("#edit_name").val(); 230 var class_ids = $("#edit_classId").val(); 231 $.ajax({ 232 url:"/modal_edit_teacher/", 233 type:"post", 234 dataType:"JSON", 235 traditional:true, 236 data:{"tid":tid,"name":name,"del_class_id":del_class_id}, 237 $.ajax({ 238 url:"/modal_edit_teacher/", 239 type:"post", 240 dataType:"JSON", 241 traditional:true, 242 data:{"tid":tid,"name":name,"class_ids":class_ids}, 243 success:function (arg) { 244 if (arg.status){ 245 location.reload(); 246 }else { 247 alert("111") 248 } 249 } 250 }) 251 }) 252 }) 253 }) 254 255 256 script> 257 258 body> 259 html> 260 261 teacher.html
1 #显示 2 def teacher(request): 3 4 tk = request.COOKIES.get("ticket") 5 if not tk: 6 return redirect("/login/") 7 8 teacher_list=sqlheper.get_list(""" 9 select teacher.tid as tid,teacher.name,class.title from teacher 10 left join teacher_class on teacher_class.teacher_id=teacher.tid 11 left join class on class.cid=teacher_class.class_id""",[]) 12 # print(teacher_list) 13 result = {} 14 for row in teacher_list: 15 tid = row["tid"] 16 if tid in result: 17 result[tid]["titles"].append(row["title"]) 18 else: 19 result[tid] = {"tid":row["tid"],"name":row["name"],"titles":[row["title"],]} 20 21 22 return render(request, "teacher.html", {"teacher_list":result.values()}) 23 24 #增加 25 def get_all_class(request): 26 import time 27 time.sleep(1) 28 obj = sqlheper.SqlHelper() 29 class_list = obj.get_list('select cid,title from class',[]) 30 obj.close() 31 return HttpResponse(json.dumps(class_list)) 32 33 def new_teacher(request): 34 35 ret = {'status': True, 'message': None} 36 try: 37 class_list=request.POST.getlist("class_list") 38 tname=request.POST.get("tname") 39 teacher_id=sqlheper.get_IncrementId("insert into teacher(name) values(%s)",[tname,]) 40 41 data_list = [] 42 for cls_id in class_list: 43 temp = (teacher_id,cls_id,) 44 data_list.append(temp) 45 46 obj = sqlheper.SqlHelper() 47 obj.multiple_modify('insert into teacher_class(teacher_id,class_id) values(%s,%s)', data_list) 48 obj.close() 49 except Exception as e: 50 ret['status'] = False 51 ret['message'] = str(e) 52 return HttpResponse(json.dumps(ret)) 53 54 55 #编辑 56 def edit_tea_cls(request): 57 58 id = request.POST.get("id") 59 obj = sqlheper.SqlHelper() 60 class_list = obj.get_list('select cid,title from class',[]) 61 62 teacher_info = obj.get_one("select tid,name from teacher where tid=%s",[id,]) 63 64 class_id = obj.get_list("select class_id from teacher_class where teacher_id=%s",[id,]) 65 66 data_list = [] 67 for cls_id in class_id: 68 data_list.append(cls_id["class_id"]) 69 70 71 72 print(teacher_info) 73 total = [] 74 total.append(class_list) 75 total.append(teacher_info) 76 total.append(data_list) 77 obj.close() 78 return HttpResponse(json.dumps(total)) 79 80 def modal_edit_teacher(request): 81 ret = {'status': True, 'message': None} 82 try: 83 name = request.POST.get("name") 84 tid = request.POST.get("tid") 85 class_ids = request.POST.getlist("class_ids") 86 87 88 obj = sqlheper.SqlHelper() 89 obj.modify("update teacher set name=%s where tid=%s", [name, tid, ]) 90 obj.modify('delete from teacher_class where teacher_id=%s', [tid, ]) 91 data_list = [] 92 for cls_id in class_ids: 93 temp = (tid, cls_id) 94 data_list.append(temp) 95 obj.multiple_modify('insert into teacher_class(teacher_id,class_id) values(%s,%s)', data_list) 96 obj.close() 97 except Exception as e: 98 ret['status'] = False 99 ret['message'] = str(e) 100 return HttpResponse(json.dumps(ret)) 101 102 views