def depart_edit(request, edit_id): edit_obj = models.Department.objects.filter(id=edit_id).first() form_obj = DepartModelForm(instance=edit_obj) if request.method == 'POST': form_obj = DepartModelForm(data=request.POST, instance=edit_obj) if form_obj.is_valid(): form_obj.save() return redirect(rev_url(request, 'depart_list')) return render(request, 'depart_form.html', {'form_obj': form_obj})
def depart_add(request): form_obj = DepartModelForm() if request.method == 'POST': form_obj = DepartModelForm(data=request.POST) if form_obj.is_valid(): # 对数据进行校验 # 保存数据 # models.Department.objects.create(**form_obj.cleaned_data) form_obj.save() return redirect(rev_url(request, 'depart_list')) return render(request, 'depart_form.html', {'form_obj': form_obj})
def classes(request, edit_id=None): obj = models.ClassList.objects.filter(pk=edit_id).first() form_obj = ClassForm(instance=obj) title = '编辑班级' if edit_id else '新增班级' if request.method == 'POST': form_obj = ClassForm(request.POST, instance=obj) if form_obj.is_valid(): form_obj.save() return redirect(rev_url(request, 'class_list')) return render(request, 'form.html', {'form_obj': form_obj, 'title': title})
def customer_change(request, edit_id=None): obj = models.Customer.objects.filter(pk=edit_id).first() form_obj = CustomerForm(instance=obj) if request.method == 'POST': form_obj = CustomerForm(request.POST, instance=obj) if form_obj.is_valid(): form_obj.save() # 没有instance新增 有instance做修改 # return redirect(reverse('customer_list')) # return redirect(reverse_url(request, 'customer_list')) return redirect(rev_url(request, 'customer_list')) return render(request, 'consultant/customer_change.html', {'form_obj': form_obj, 'edit_id': edit_id})
def course_record(request, class_id=None, course_record_id=None): obj = models.CourseRecord.objects.filter(pk=course_record_id).first( ) if course_record_id else models.CourseRecord(re_class_id=class_id, teacher=request.account) form_obj = CourseRecordForm(instance=obj) title = '编辑课程记录' if course_record_id else '新增课程记录' if request.method == 'POST': form_obj = CourseRecordForm(request.POST, instance=obj) if form_obj.is_valid(): form_obj.save() return redirect( rev_url(request, 'course_record_list', obj.re_class_id if course_record_id else class_id)) return render(request, 'form.html', {"form_obj": form_obj, "title": title})
def modify_customer(request, edit_id=None): # 通过id获取对象 obj = models.Customer.objects.filter(pk=edit_id).first() # 获取form对象, 发送给模板渲染,如果获取的是none,相当于新增 form_obj = CustomerForm(instance=obj) # 接收到POST请求数据 if request.method == 'POST': # 把POST请求的数据替换原来的数据. 如果原来的对象是None, 相当于新增 form_obj = CustomerForm(request.POST, instance=obj) # 通过校验 if form_obj.is_valid(): form_obj.save() # 没有instance新增,有instance做修改 return redirect(rev_url(request, 'customer_list')) return render(request, 'consultant/modify_customer.html', {'form_obj': form_obj, 'edit_id': edit_id})
def course_record(request, class_id=None, course_record_id=None): # 如果有课程记录的id, 进入编辑课程记录模式, 获取课程记录对象. 如果没有课程记录id, 进入增加课程记录模式, 创建虚拟对象 obj = models.CourseRecord.objects.filter(pk=course_record_id).first( ) if course_record_id else models.CourseRecord(re_class_id=class_id, teacher=request.account) form_obj = CourseRecordForm(instance=obj) title = '编辑课程记录' if course_record_id else '添加课程记录' if request.method == 'POST': form_obj = CourseRecordForm(request.POST, instance=obj) if form_obj.is_valid(): form_obj.save() return redirect( rev_url(request, 'course_record_list', obj.re_class_id if course_record_id else class_id)) return render(request, 'teacher/course_record.html', { 'form_obj': form_obj, 'title': title })