from django.shortcuts import render # Create your views here. # Create your views here. from django.http import JsonResponse from .models import Teacher from django.views.decorators.csrf import csrf_exempt from django.http import QueryDict @csrf_exempt def teacher(request): if request.method == "POST": teacher_information = Teacher() teacher_information.t_name = request.POST.get('t_name') teacher_information.t_sex = request.POST.get('t_sex') teacher_information.t_title = request.POST.get('t_title') teacher_information.t_education = request.POST.get('t_education') teacher_information.t_dept = request.POST.get('t_dept') teacher_information.save() print(teacher_information.t_name) return JsonResponse({'code': 200, 'msg': 'success',"data": teacher_information.to_dict()}, safe=False) elif request.method == "GET": t_name = request.GET.get('t_name') t_sex = request.GET.get('t_sex') t_title = request.GET.get('t_title') t_education = request.GET.get('t_education') t_dept = request.GET.get('t_dept') data = [] if t_name: filtered = Teacher.objects.filter(t_name=t_name) for teacher_x in filtered: data.append(teacher_x.to_dict()) elif t_sex: filtered = Teacher.objects.filter(t_sex=t_sex) for teacher_x in filtered: data.append(teacher_x.to_dict()) elif t_title: filtered = Teacher.objects.filter(t_title=t_title) for teacher_x in filtered: data.append(teacher_x.to_dict()) elif t_education: filtered = Teacher.objects.filter(t_education=t_education) for teacher_x in filtered: data.append(teacher_x.to_dict()) elif t_dept: filtered = Teacher.objects.filter(t_dept=t_dept) for teacher_x in filtered: data.append(teacher_x.to_dict()) else: all_objects = Teacher.objects.all() for teacher_x in all_objects: data.append(teacher_x.to_dict()) return JsonResponse({'code': 200, 'msg': 'success', 'data': data}, safe=False)