目标,创建学生管理,使学生表格与班级表格连接,一个班级可以创建多个学生
学生管理页面需要增删改查
1、首先在url中增加
path('students.html', students.get_students), path('add_students.html', students.add_students), path('del_students.html', students.del_students), path('edit_students.html', students.edit_students),
2、在views文件夹中编辑students.py文件
from django.shortcuts import render,redirect from app01 import models #查询页面 def get_students(request): #获取学生表格的id、名字、年龄、性别、班级名称(跨表查询) stu_lis = models.Students.objects.all().values('id','username','age','gender','cs__title') return render(request, 'get_students.html', {'stu_lis': stu_lis}) #增加学生页面 def add_students(request): #获取班级列表 cls_list = models.Classes.objects.all() #判断首次进入采用get方式 if request.method == 'GET': #将班级列表返回至前台 return render(request,'add_students.html', {'cls_list': cls_list}) #判断通过POST方式提交 elif request.method == 'POST': #从前台获取学生名字、年龄、性别、班级 stu_name = request.POST.get('stu_name') stu_age = request.POST.get('stu_age') stu_gender = request.POST.get('stu_gender') stu_cls = request.POST.get('stu_class') #将内容更新值数据库中 models.Students.objects.create(username=stu_name,age=stu_age,gender=stu_gender,cs_id=stu_cls) return redirect('students.html') #删除功能 def del_students(request): #通过前台获取目标学生id编号 sid = request.GET.get('sid') #操作数据库删除 models.Students.objects.filter(id=sid).delete() return redirect('students.html') #修改功能 def edit_students(request): cls_list = models.Classes.objects.values('id','title') if request.method == 'GET': sid = request.GET.get('sid') #通过前台获取的学生id编号,获取目标学生的对象信息 stu_one = models.Students.objects.filter(id=sid).first() return render(request,'edit_students.html',{'stu_one':stu_one,'cls_list':cls_list}) elif request.method == 'POST': sid = request.GET.get('eid') # print(sid) username = request.POST.get('stu_name') age = request.POST.get('stu_age') gender = request.POST.get('stu_gender') cs_id = request.POST.get('stu_class') # print(username,age,gender,cs_id) models.Students.objects.filter(id=sid).update(username=username,age=age,gender=gender,cs_id=cs_id) return redirect('students.html')
3、编辑get_students.html
<body> <div> <a href="/add_students.html">添加</a> <table border="1"> <thead> <tr> <th>ID</th> <th>姓名</th> <th>年龄</th> <th>性别</th> <th>班级</th> <th>操作</th> </tr> </thead> <tbody> {% for row in stu_lis%} <tr> <td>{{ row.id }}</td> <td>{{ row.username }}</td> <td>{{ row.age }}</td> <td> {% if row.gender == 0 %} 男 {% elif row.gender == 1 %} 女 {% endif %} </td> <td>{{ row.cs__title }}</td> <td><a href="/del_students.html?sid={{ row.id }}">删除</a> | <a href="/edit_students.html?sid={{ row.id }}">修改</a> </td> </tr> {% endfor %} </tbody> </table> </div> </body>
4、编辑add_students.html
<body> <form action="/add_students.html" method="post"> {% csrf_token %} <table border="1"> <tr> <td><input type="text" name="stu_name" />姓名</td> </tr> <tr> <td><input type="text" name="stu_age" />年龄</td> </tr> <tr> <td><input type="radio" name="stu_gender" value="0" />男 | <input type="radio" name="stu_gender" value="1" />女 </td> </tr> <tr> <td> <select name="stu_class"> {% for row in cls_list %} <option value="{{ row.id }}">{{ row.title }}</option> {% endfor %} </select> 班级 </td> </tr> <tr> <td><input type="submit" value="提交" /></td> </tr> </table> </form> </body>
5、编辑edit_students.html
<body> <form action="/edit_students.html?eid={{ stu_one.id }}" method="post"> {% csrf_token %} <table border="1"> <tr> <td><input type="text" name="stu_name" value="{{ stu_one.username }}"/>姓名</td> </tr> <tr> <td><input type="text" name="stu_age" value="{{ stu_one.age }}"/>年龄</td> </tr> <tr> <td><input type="radio" name="stu_gender" value="0" {% if stu_one.gender == 0%} checked {% endif %} />男 | <input type="radio" name="stu_gender" value="1" {% if stu_one.gender == 1%} checked {% endif %} />女 </td> </tr> <tr> <td> <select name="stu_class"> {% for row in cls_list %} <option value="{{ row.id }}" {% if stu_one.cs_id == row.id %} selected="selected" {% endif %} >{{ row.title }}</option> {% endfor %} </select> 班级 </td> </tr> <tr> <td><input type="submit" value="提交" /></td> </tr> </table> </form>
多对多表格,需求是在classes.html中新增老师列,并增加修改老师的功能
1、首先增加urls
path('set_teachers.html', teachers.set_teachers),
2、编辑get_classes.html,新增白框中的内容
3、编辑set_teacher.html
<body> <form action="/set_teachers.html?cid={{ cid }}" method="post"> {% csrf_token %} <select multiple size='10' name="teacher_list" id=""> {% for item in all_teacher_list %} <option value="{{ item.id }}" {% if item.id in teacher_id %} selected {% endif %} >{{ item.name }}</option> {% endfor %} </select> <input type="submit" value="提交"> </form> </body>
老师管理页面,需求:在查询页面显示老师列表,并显示每个老师管理的班级列表,修改时可修改老师姓名及管理班级
1、首先编辑urls.py
path('teachers.html', teachers.get_teachers), path('add_teachers.html', teachers.add_teachers), path('edit_teachers.html', teachers.edit_teachers), path('del_teachers.html', teachers.del_teachers),
2、编辑teachers.py
from django.shortcuts import render, redirect from app01 import models def set_teachers(request): if request.method == 'GET': # 获取前台cid cid = request.GET.get('cid') # 获取cid的班级对象 cls_obj = models.Classes.objects.filter(id=cid).first() # 通过多对多表格,获取班级对象相关的老师表格,并获取id值 cls_teacher_list = cls_obj.m.all().values_list('id', 'name') # 获取结果为[(1,'alex'),(2,'yuan')]这样的格式,可以通过zip的*方式进行解压 # 获取老师id的列表,【1,2,3】 # teacher_id = list(zip(*cls_teacher_list))[0] # 如果对应关系没有建立的时候,查不到结果,会报错误 # 采用三元运算符 判断list(zip(*cls_teacher_list))是否为True,否则赋值一个空列表 teacher_id = list(zip(*cls_teacher_list))[0] if list(zip(*cls_teacher_list)) else [] all_teacher_list = models.Teachers.objects.all() return render(request, 'set_teachers.html', { 'all_teacher_list': all_teacher_list, 'teacher_id': teacher_id, 'cid': cid, }) elif request.method == 'POST': cid = request.GET.get('cid') ids = request.POST.getlist('teacher_list') # print('当前班级的id',cid,'分配老师的id',ids) cls_obj = models.Classes.objects.filter(id=cid).first() cls_obj.m.set(ids) return redirect('/classes.html') def get_teachers(request): teacher_list = models.Teachers.objects.all() return render(request, 'get_teachers.html', {'teacher_list': teacher_list}) def add_teachers(request): if request.method == 'GET': cls_list = models.Classes.objects.all() return render(request, 'add_teachers.html', {'cls_list': cls_list}) elif request.method == 'POST': name = request.POST.get('teacher_name') teacher_class = request.POST.get('teacher_class') models.Teachers.objects.create(name=name) t_obj = models.Teachers.objects.filter(name=name).first() t_obj.sssss.add(teacher_class) return redirect('/teachers.html') def edit_teachers(request): if request.method == 'GET': sid = request.GET.get('sid') # print('sid', sid) cls_list = models.Classes.objects.all() c_l_obj = models.Teachers.objects.filter(id=sid).first() s_t_id = c_l_obj.sssss.all().values_list('id', 'title') # c_l = models.Classes.objects.all().values_list('id') c_list = list(zip(*s_t_id))[0] # print(c_list) tea_list = models.Teachers.objects.filter(id=sid).first() return render(request, 'edit_teachers.html', { 'cls_list': cls_list, 'sid': sid, 'tea_list': tea_list, 'c_list': c_list }) elif request.method == 'POST': name = request.POST.get('teacher_name') teacher_class = request.POST.getlist('teacher_class') # print(teacher_class) t_obj = models.Teachers.objects.filter(name=name).first() t_obj.sssss.set(teacher_class) return redirect('/teachers.html') def del_teachers(request): sid = request.GET.get('sid') models.Teachers.objects.filter(id=sid).delete() return redirect('/teachers.html')
3、编辑get_teachers.html
<body> <div> <a href="/add_teachers.html">添加</a> </div> <div> <table border="1"> <thead> <tr> <th>ID</th> <th>姓名</th> <th>所属班级</th> <th>操作</th> </tr> </thead> <tbody> {% for row in teacher_list %} <tr> <td> {{ row.id }} </td> <td> {{ row.name }} </td> <td> {% for class_teacher in row.sssss.all %} <span>{{ class_teacher.title }}</span> {% endfor %} </td> <td> <a href="/del_teachers.html?sid={{ row.id }}">删除</a> | <a href="/edit_teachers.html?sid={{ row.id }}">编辑</a> </td> </tr> {% endfor %} </tbody> </table> </div> </body>
4、编辑add_teachers.html
<body> <form action="/add_teachers.html" method="post"> {% csrf_token %} <p>教师姓名<input type="text" name="teacher_name" value=""></p> <p> <select name="teacher_class" id=""> {% for item in cls_list %} <option value="{{ item.id }}">{{ item.title }}</option> {% endfor %} </select> </p> <input type="submit"> </form> </body>
5、编辑edit_teachers.html
<body> <form action="/edit_teachers.html?sid{{ sid }}" method="post"> {% csrf_token %} <p>教师姓名<input type="text" name="teacher_name" value="{{ tea_list.name }}"></p> <p> <select multiple size="10" name="teacher_class" id=""> {% for item in cls_list %} <option value="{{ item.id }}" {% if item.id in c_list %} selected {% endif %} >{{ item.title }}</option> {% endfor %} </select> </p> <input type="submit"> </form> </body>