You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

99 lines
4.0 KiB

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 io import BytesIO
from django.http.multipartparser import MultiPartParser
from django.http import QueryDict
import json
@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)
elif request.method == "DELETE":
parser = MultiPartParser(request.META, BytesIO(request.body), request.upload_handlers, request.encoding)
post_dict = parser.parse()
print(post_dict)
tid = int(post_dict[0]['tid'])
print(tid)
try:
info = Teacher.objects.filter(tid=tid).get().to_dict()
Teacher.objects.filter(tid=tid).delete()
response = {"code": 200, "message": "删除成功!", "data": info}
return JsonResponse(response)
except:
response = {"code": 200, "message": "删除失败,未找到老师信息!"}
return JsonResponse(response)
elif request.method == 'PUT':
put = MultiPartParser(request.META, request, request.upload_handlers, request.encoding).parse()
# request.PUT = put[0]
print(put)
tid = put[0]['tid']
t_name = put[0]['t_name']
t_sex = put[0]['t_sex']
t_title = put[0]['t_title']
t_education = put[0]['t_education']
t_dept = put[0]['t_dept']
Teacher.objects.filter(tid=tid).update(t_name=t_name)
Teacher.objects.filter(tid=tid).update(t_sex=t_sex)
Teacher.objects.filter(tid=tid).update(t_title=t_title)
Teacher.objects.filter(tid=tid).update(t_education=t_education)
Teacher.objects.filter(tid=tid).update(t_dept=t_dept)
data = Teacher.objects.filter(tid=tid)[0].to_dict()
return JsonResponse({'code': 200, 'msg': 'success','data':data}, safe=False)
def delete(request):
tid = request.GET.get('tid')
Teacher.objects.filter(tid = tid).delete()
return JsonResponse({'code': 200, 'msg': 'success'}, safe=False)