• teacher列表的视图函数


    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})
    

      

  • 相关阅读:
    001 Python网络爬虫与信息提取 课程前序
    004 JQuery (010
    Vuex的元素对象
    003 JQuery (009
    002 JQuery (004
    001 JQuery (001
    Vuex简介
    axios实例与模块封装
    axios拦截器
    015 Javascript(152
  • 原文地址:https://www.cnblogs.com/mainstream/p/11006127.html
Copyright © 2020-2023  润新知