commit teacher

scl-branch
lrs 1 year ago
parent 16ff43e0ec
commit 2bb816a4ef

@ -10,5 +10,6 @@ from django.urls import path
from .views import *
urlpatterns = [
path("", teacher)
path("", teacher),
path('delete/',delete)
]

@ -6,7 +6,10 @@ from django.shortcuts import render
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":
@ -51,4 +54,45 @@ def teacher(request):
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)
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)

Loading…
Cancel
Save