From 48687130954ac7abb36987c10e80eea6b32bb22c Mon Sep 17 00:00:00 2001 From: bettleChen <2207153529@qq.com> Date: Wed, 6 Sep 2023 15:07:42 +0800 Subject: [PATCH] create get student function --- EduSystemServer/EduSystemServer/settings.py | 1 - EduSystemServer/EduSystemServer/urls.py | 2 +- EduSystemServer/Student/models.py | 3 +++ EduSystemServer/Student/urls.py | 7 +++++ EduSystemServer/Student/views.py | 29 +++++++++++++++++++++ 5 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 EduSystemServer/Student/urls.py diff --git a/EduSystemServer/EduSystemServer/settings.py b/EduSystemServer/EduSystemServer/settings.py index 34c413d..e8d1b6c 100644 --- a/EduSystemServer/EduSystemServer/settings.py +++ b/EduSystemServer/EduSystemServer/settings.py @@ -89,7 +89,6 @@ DATABASES = { } } - # Password validation # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators diff --git a/EduSystemServer/EduSystemServer/urls.py b/EduSystemServer/EduSystemServer/urls.py index 8e2f8a4..2b2d6da 100644 --- a/EduSystemServer/EduSystemServer/urls.py +++ b/EduSystemServer/EduSystemServer/urls.py @@ -16,9 +16,9 @@ Including another URLconf from django.contrib import admin from django.urls import path, include -import API.urls urlpatterns = [ path('Eduadmin/', admin.site.urls), path('api/', include(("API.urls", "api"), namespace="api")), + path('student/', include(("Student.urls", "studnet"), namespace="student")), ] diff --git a/EduSystemServer/Student/models.py b/EduSystemServer/Student/models.py index 765a860..e3a2b11 100644 --- a/EduSystemServer/Student/models.py +++ b/EduSystemServer/Student/models.py @@ -12,6 +12,9 @@ class Student(models.Model): class_name = models.CharField(max_length=50, verbose_name="班级", name="s_class_name") major = models.CharField(max_length=50, verbose_name="专业名称", name="s_major") + def to_dict(self): + return {"sid": self.sid, "s_name": self.s_name} + class Meta: db_table = "student" verbose_name = "学生" diff --git a/EduSystemServer/Student/urls.py b/EduSystemServer/Student/urls.py new file mode 100644 index 0000000..78f4a6b --- /dev/null +++ b/EduSystemServer/Student/urls.py @@ -0,0 +1,7 @@ +from django.urls import path + +from Student.views import * + +urlpatterns = [ + path("", studnets) +] \ No newline at end of file diff --git a/EduSystemServer/Student/views.py b/EduSystemServer/Student/views.py index 91ea44a..d33cf19 100644 --- a/EduSystemServer/Student/views.py +++ b/EduSystemServer/Student/views.py @@ -1,3 +1,32 @@ from django.shortcuts import render # 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) + +