学员管理系统之老师管理,老师表字段teacher_id,tname,t2c(django自动生成的多对多第三张表),要在前端的老师表中显示出老师id,老师姓名和老师所教班级,所教班级要
通过对象obj的t2c来关联,obj.t2c.all(),获取到第三张表的多有对象(在一个jquerry set中),obj.t2c.all()在前端的表示是obj.t2c.all,将这个jquery set循环一下获取到第三张班级表
的班级名字,就可以了详见html代码
def teacher(request): obj = models.Teacher.objects.all() return render(request, "teacher.html", {"obj":obj})
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>老师管理</h1> <a href="/add_teacher.html/">添加老师</a> <table> <tr> <th>老师ID</th> <th>老师姓名</th> <th>任教班级</th> <th>操作</th> </tr> {% for row in obj %} <tr> <td>{{ row.id }}</td> <td>{{ row.tname }}</td> <td> {% for item in row.c2t.all %} # 重点是这里 <span>{{ item.name }}</span> {% endfor %} </td> <td> <a>编辑</a> <a>删除</a> </td> </tr> {% endfor %} </table> </body> </html>
class ClassForm(Form): title = fields.RegexField('全栈d+') def class_list(request): cls_list = models.Classes.objects.all() return render(request,'class_list.html',{'cls_list':cls_list}) def add_class(request): if request.method == "GET": obj = ClassForm() return render(request,'add_class.html',{'obj': obj}) else: obj = ClassForm(request.POST) if obj.is_valid(): # obj.cleaned_data # 字典 # 数据库创建一条数据 # print(obj.cleaned_data) # models.Classes.objects.create(title=obj.cleaned_data['tt']) models.Classes.objects.create(**obj.cleaned_data) return redirect('/class_list/') return render(request,'add_class.html',{'obj': obj}) def edit_class(request,nid): if request.method == "GET": row = models.Classes.objects.filter(id=nid).first() # 让页面显示初始值 # obj = ClassForm(data={'title': 'asdfasdfasdfas'}) obj = ClassForm(initial={'title': row.title}) return render(request,'edit_class.html',{'nid': nid,'obj':obj}) else: obj = ClassForm(request.POST) if obj.is_valid(): models.Classes.objects.filter(id=nid).update(**obj.cleaned_data) return redirect('/class_list/') return render(request,'edit_class.html',{'nid': nid,'obj':obj}) class StudentForm(Form): name = fields.CharField( min_length=2, max_length=6, widget=widgets.TextInput(attrs={'class': 'form-control'}) ) email = fields.EmailField(widget=widgets.TextInput(attrs={'class': 'form-control'})) age = fields.IntegerField(min_value=18,max_value=25,widget=widgets.TextInput(attrs={'class': 'form-control'})) cls_id = fields.IntegerField( # widget=widgets.Select(choices=[(1,'上海'),(2,'北京')]) widget=widgets.Select(choices=models.Classes.objects.values_list('id','title'),attrs={'class': 'form-control'}) ) def student_list(request): stu_list = models.Student.objects.all() return render(request,'student_list.html',{'stu_list':stu_list}) def add_student(request): if request.method == "GET": obj = StudentForm() return render(request,'add_student.html',{'obj':obj}) else: obj = StudentForm(request.POST) if obj.is_valid(): models.Student.objects.create(**obj.cleaned_data) return redirect('/student_list/') return render(request,'add_student.html',{'obj':obj}) def edit_student(request,nid): if request.method == "GET": row = models.Student.objects.filter(id=nid).values('name','email','age','cls_id').first() obj = StudentForm(initial=row) return render(request,'edit_student.html',{'nid':nid,'obj': obj}) else: obj = StudentForm(request.POST) if obj.is_valid(): models.Student.objects.filter(id=nid).update(**obj.cleaned_data) return redirect('/student_list/') return render(request,'edit_student.html',{'nid':nid,'obj': obj}) def teacher_list(request): tea_list = models.Teacher.objects.all() return render(request,'teacher_list.html',{'tea_list':tea_list}) from django.forms import models as form_model class TeacherForm(Form): tname = fields.CharField(min_length=2) # xx = form_model.ModelMultipleChoiceField(queryset=models.Classes.objects.all()) # xx = form_model.ModelChoiceField(queryset=models.Classes.objects.all()) xx = fields.MultipleChoiceField( # choices=models.Classes.objects.values_list('id','title'), widget=widgets.SelectMultiple ) def __init__(self,*args,**kwargs): super(TeacherForm,self).__init__(*args,**kwargs) self.fields['xx'].widget.choices = models.Classes.objects.values_list('id','title') # obj = TeacherForm() # 1. 找到所有字段 # 2. self.fields = { # tname: fields.CharField(min_length=2) # } def add_teacher(request): if request.method == "GET": obj = TeacherForm() return render(request,'add_teacher.html',{'obj':obj}) else: obj = TeacherForm(request.POST) if obj.is_valid(): xx = obj.cleaned_data.pop('xx') row = models.Teacher.objects.create(**obj.cleaned_data) row.c2t.add(*xx) # [1,2] return redirect('/teacher_list/') return render(request,'add_teacher.html',{'obj':obj}) def edit_teacher(request,nid): # 重点这块 if request.method == "GET": row = models.Teacher.objects.filter(id=nid).first() class_ids = row.c2t.values_list('id') # print(class_ids) # id_list = [] id_list = list(zip(*class_ids))[0] if list(zip(*class_ids)) else [] # obj = TeacherForm(initial={'tname':row.tname,'xx':[1,2,3]}) obj = TeacherForm(initial={'tname':row.tname,'xx':id_list}) return render(request,'edit_teacher.html',{'obj':obj}) #