teacher列表的增、删、改、查函数
def teacher_list(request): teacher_list = models.Teacher.objects.all() return render(request,'teacher_list.html',{'teacher_list':teacher_list}) def delete_teacher(request): tid = request.GET.get('teacher_id') models.Teacher.objects.filter(id=tid).delete() return redirect('/teacher_list/') ''' #添加老师方法二: def add_teacher(request): if request.method == "POST": tname = request.POST.get("tname") class_ids = request.POST.getlist("cid") #去数据库插入一条数据 teacher_obj = models.Teacher.objects.create(tname=tname) # 直接添加ID # teacher_obj.cid.add(*class_ids) #class_ids = [1,2,3] #teacher_obj.cid.add(*class_ids) #第二种 方式 添加的是class对象 class_objs = models.Class.objects.filter(id__in=class_ids) #设置班级关系,因为是新的老师所以 用set方式设置 teacher_obj.cid.set(class_objs) return redirect('/teacher_list/') class_list = models.Class.objects.all() return render(request,'add_teacher.html',{"class_list":class_list}) ''' ''' 添加老师方法一 def add_teacher(request): if request.method == "POST": tname = request.POST.get("tname") class_ids = request.POST.getlist("cid") #去数据库插入一条数据 teacher_obj = models.Teacher.objects.create(tname=tname) # 直接添加ID # teacher_obj.cid.add(*class_ids) #class_ids = [1,2,3] teacher_obj.cid.add(*class_ids) return redirect('/teacher_list/') class_list = models.Class.objects.all() return render(request,'add_teacher.html',{"class_list":class_list}) #添加的是class对象 #class_objs = models.Class.objects.filter(id__in=class_ids) # teacher_obj.cid.add(*class_obj) #class_objs = [<Class obj(1)>,<Class obj(2)>,<Class obj(3)>] #设置班级关系,因为是新的老师,所以可以直接用set来设置 #teacher_obj.cid.set(class_objs) #class_objs = [<Class obj(1)>,<Class obj(2)>,<Class obj(3)>] ''' ''' def edit_teacher(request): if request.method == "POST": id = request.POST.get("id") tname = request.POST.get("tname") class_ids = request.POST.getlist("cid") models.Teacher.objects.filter(id=id).update(tname=tname) obj = models.Teacher.objects.get(id=id) # obj.tname = tname # obj.save() obj.cid.set(class_ids) return redirect(reverse('teacher_list')) tid = request.GET.get("teacher_id") teacher_obj = models.Teacher.objects.get(id=tid) class_list = models.Class.objects.all() return render(request,'edit_teacher.html',{'teacher':teacher_obj,'class_list':class_list}) ''' def add_teacher(request): if request.method == "POST": tname = request.POST.get("tname") class_ids = request.POST.getlist("cid") teacher_obj = models.Teacher.objects.create(tname=tname) class_objs = models.Class.objects.filter(id__in=class_ids) teacher_obj.cid.set(class_objs) return redirect(reverse('teacher_list')) class_list = models.Class.objects.all() return render(request,'add_teacher.html',{"class_list":class_list}) def edit_teacher(request): if request.method == "POST": id = request.POST.get("id") tname = request.POST.get("tname") models.Teacher.objects.filter(id=id).update(tname=tname) obj = models.Teacher.objects.get(id=id) class_ids = request.POST.getlist("cid") obj.cid.set(class_ids) return redirect(reverse('teacher_list')) tid = request.GET.get("teacher_id") teacher_obj = models.Teacher.objects.get(id=tid) class_list = models.Class.objects.all() return render(request, 'edit_teacher.html', {'teacher': teacher_obj, 'class_list': class_list})