parent
aac62e2ac7
commit
4868713095
@ -0,0 +1,7 @@
|
|||||||
|
from django.urls import path
|
||||||
|
|
||||||
|
from Student.views import *
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
path("", studnets)
|
||||||
|
]
|
@ -1,3 +1,32 @@
|
|||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
|
|
||||||
# Create your views here.
|
# Create your views here.
|
||||||
|
from django.http import JsonResponse
|
||||||
|
from Student.models import Student
|
||||||
|
from django.views.decorators.csrf import csrf_exempt
|
||||||
|
|
||||||
|
@csrf_exempt
|
||||||
|
def studnets(request):
|
||||||
|
if request.method == "POST":
|
||||||
|
student = Student()
|
||||||
|
student.username = request.POST.get("username")
|
||||||
|
student.password = request.POST.get("password")
|
||||||
|
student.s_name = request.POST.get("s_name")
|
||||||
|
student.sex = request.POST.get("sex")
|
||||||
|
student.grade = int(request.POST.get("grade"))
|
||||||
|
print(student.grade)
|
||||||
|
student.class_name = request.POST.get("class_name")
|
||||||
|
student.major = request.POST.get("major")
|
||||||
|
student.sid = request.POST.get("sid")
|
||||||
|
print(student.to_dict())
|
||||||
|
student.save()
|
||||||
|
response = {"code": 200, "message": "添加成功!", "data": student.to_dict()}
|
||||||
|
return JsonResponse(response)
|
||||||
|
elif request.method == "GET":
|
||||||
|
student_list = Student.objects.all()
|
||||||
|
response_json = {"code": 200, "message": "success", "data": []}
|
||||||
|
for student in student_list:
|
||||||
|
response_json["data"].append(student.to_dict())
|
||||||
|
return JsonResponse(response_json)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in new issue