create get student function

scl-branch
bettleChen 1 year ago
parent aac62e2ac7
commit 4868713095

@ -89,7 +89,6 @@ DATABASES = {
} }
} }
# Password validation # Password validation
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators # https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators

@ -16,9 +16,9 @@ Including another URLconf
from django.contrib import admin from django.contrib import admin
from django.urls import path, include from django.urls import path, include
import API.urls
urlpatterns = [ urlpatterns = [
path('Eduadmin/', admin.site.urls), path('Eduadmin/', admin.site.urls),
path('api/', include(("API.urls", "api"), namespace="api")), path('api/', include(("API.urls", "api"), namespace="api")),
path('student/', include(("Student.urls", "studnet"), namespace="student")),
] ]

@ -12,6 +12,9 @@ class Student(models.Model):
class_name = models.CharField(max_length=50, verbose_name="班级", name="s_class_name") class_name = models.CharField(max_length=50, verbose_name="班级", name="s_class_name")
major = models.CharField(max_length=50, verbose_name="专业名称", name="s_major") 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: class Meta:
db_table = "student" db_table = "student"
verbose_name = "学生" verbose_name = "学生"

@ -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…
Cancel
Save