Compare commits

...

3 Commits

@ -1,7 +1,9 @@
from django.shortcuts import render
from django.http import JsonResponse
# Create your views here.
def student(request):
return JsonResponse({"sid": 1, "sname":"张三"})
return JsonResponse({"sid": 1, "sname": "张三"})

@ -20,5 +20,6 @@ from django.urls import path, include
urlpatterns = [
path('Eduadmin/', admin.site.urls),
path('api/', include(("API.urls", "api"), namespace="api")),
path('student/', include(("Student.urls", "studnet"), namespace="student")),
path('student/', include(("Student.urls", "student"), namespace="student")),
path('course/', include(("course.urls", "course"), namespace="course"))
]

@ -4,17 +4,17 @@ from django.db import models
# Create your models here.
class Student(models.Model):
sid = models.AutoField(primary_key=True, verbose_name="学生编号", name="sid")
username = models.CharField(max_length=30, verbose_name="用户名称", name="s_username", blank=True)
password = models.CharField(max_length=100, verbose_name="密码", name="s_password", blank=True)
s_username = models.CharField(max_length=30, verbose_name="用户名称", name="s_username", blank=True)
s_password = models.CharField(max_length=100, verbose_name="密码", name="s_password", blank=True)
s_name = models.CharField(max_length=100, verbose_name="姓名", name="s_name", blank=True)
sex = models.CharField(max_length=4, verbose_name="性别", name="s_sex", blank=True)
grade = models.CharField(max_length=20, verbose_name="年级", name="s_grade", blank=True)
class_name = models.CharField(max_length=50, verbose_name="班级", name="s_class_name", blank=True)
major = models.CharField(max_length=50, verbose_name="专业名称", name="s_major", blank=True)
s_sex = models.CharField(max_length=4, verbose_name="性别", name="s_sex", blank=True)
s_grade = models.CharField(max_length=20, verbose_name="年级", name="s_grade", blank=True)
s_class_name = models.CharField(max_length=50, verbose_name="班级", name="s_class_name", blank=True)
s_major = models.CharField(max_length=50, verbose_name="专业名称", name="s_major", blank=True)
def to_dict(self):
return {"sid": self.sid, "s_name": self.s_name, "username": self.username, "password": self.password,
"s_sex": self.sex, "s_grade": self.grade, "class_name": self.class_name, "major": self.major}
return {"sid": self.sid, "s_name": self.s_name, "s_username": self.s_username, "s_password": self.s_password,
"s_sex": self.s_sex, "s_grade": self.s_grade, "s_class_name": self.s_class_name, "s_major": self.s_major}
class Meta:
db_table = "student"

@ -3,5 +3,5 @@ from django.urls import path
from Student.views import *
urlpatterns = [
path("", studnets)
path("", studnets),
]

@ -4,29 +4,89 @@ from django.shortcuts import render
from django.http import JsonResponse
from Student.models import Student
from django.views.decorators.csrf import csrf_exempt
from io import BytesIO
from django.http.multipartparser import MultiPartParser
@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.s_username = request.POST.get("s_username")
student.s_password = request.POST.get("s_password")
student.s_name = request.POST.get("s_name") or ''
student.s_sex = request.POST.get("s_sex") or ''
student.s_grade = request.POST.get("s_grade") or ''
student.s_class_name = request.POST.get("s_class_name") or ''
student.s_major = request.POST.get("s_major") or ''
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)
sid = request.GET.get('sid')
s_name = request.GET.get('s_name')
s_sex = request.GET.get('s_sex')
s_grade = request.GET.get('s_grade')
s_class_name = request.GET.get('s_class_name')
s_major = request.GET.get('s_major')
data = []
if sid:
filtered = Student.objects.filter(sid=sid)
for student in filtered:
data.append(student.to_dict())
elif s_name:
filtered = Student.objects.filter(s_name=s_name)
for student in filtered:
data.append(student.to_dict())
elif s_sex:
filtered = Student.objects.filter(s_sex=s_sex)
for student in filtered:
data.append(student.to_dict())
elif s_grade:
filtered = Student.objects.filter(s_grade=s_grade)
for student in filtered:
data.append(student.to_dict())
elif s_class_name:
filtered = Student.objects.filter(s_class_name=s_class_name)
for student in filtered:
data.append(student.to_dict())
elif s_major:
filtered = Student.objects.filter(s_major=s_major)
for student in filtered:
data.append(student.to_dict())
else:
all_objects = Student.objects.all()
for studnet in all_objects:
data.append(studnet.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()
sid = post_dict[0]['sid']
try:
info = Student.objects.filter(sid=sid).get().to_dict()
Student.objects.filter(sid=sid).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()
sid = put[0]['sid']
s_username = put[0]['s_username']
s_name = put[0]['s_name']
s_sex = put[0]['s_sex']
s_grade = put[0]['s_grade']
s_class_name = put[0]['s_class_name']
s_major = put[0]['s_major']
try:
Student.objects.filter(sid=sid).update(s_username=s_username, s_name=s_name, s_sex=s_sex, s_grade=s_grade,
s_class_name=s_class_name,
s_major=s_major)
except:
Student.objects.create(sid=sid, s_username=s_username, s_name=s_name, s_sex=s_sex, s_grade=s_grade,
s_class_name=s_class_name,
s_major=s_major)
data = Student.objects.filter(sid=sid)[0].to_dict()
return JsonResponse({'code': 200, 'msg': 'success', 'data': data}, safe=False)

@ -12,6 +12,10 @@ class Course(models.Model):
credit = models.IntegerField(verbose_name="课程学分", name="credit")
tid = models.ForeignKey(Teacher, to_field="tid", on_delete=models.CASCADE, name="tid")
def to_dict(self):
return {"cid": self.cid, "c_name": self.c_name, "type": self.type, "credit": self.credit,
"tid": self.tid}
class Meta:
db_table = "course"
verbose_name = "课程"
@ -24,6 +28,9 @@ class SC(models.Model):
middle_grade = models.IntegerField(name="middle_grade")
end_grade = models.IntegerField(name="end_grade")
def to_dict(self):
return {"sid": self.sid, "cid": self.cid, "middle_grade": self.middle_grade, "end_grade": self.end_grade}
class Meta:
db_table = "sc"
verbose_name = "管理员"

@ -0,0 +1,7 @@
from django.urls import path
from course.views import *
urlpatterns = [
path("", courses),
]

@ -1,3 +1,69 @@
from django.shortcuts import render
# Create your views here.
from django.http import JsonResponse
from course.models import *
from django.views.decorators.csrf import csrf_exempt
from io import BytesIO
from django.http.multipartparser import MultiPartParser
@csrf_exempt
def courses(request):
if request.method == "POST":
course = Course()
course.c_name = request.POST.get("c_name")
course.type = request.POST.get('type')
course.credit = request.POST.get("credit")
course.tid = request.POST.get('tid')
course.save()
response = {"code": 200, "message": "添加成功!", "data": course.to_dict()}
return JsonResponse(response)
elif request.method == "GET":
cid = request.GET.get('cid')
c_name = request.GET.get('c_name')
c_type = request.GET.get('type')
tid = request.GET.get('tid')
data = []
if cid:
filtered = Course.objects.filter(cid=cid)
for item in filtered:
data.append(item.to_dict())
elif c_name:
filtered = Course.objects.filter(c_name=c_name)
for item in filtered:
data.append(item.to_dict())
elif c_type:
filtered = Course.objects.filter(type=c_type)
for item in filtered:
data.append(item.to_dict())
elif tid:
filtered = Course.objects.filter(tid=tid)
for item in filtered:
data.append(item.to_dict())
else:
all_objects = Course.objects.all()
for item in all_objects:
data.append(item.to_dict())
return JsonResponse({'code': 200, 'msg': 'success', 'data': data}, safe=False)
elif request.method == "DELETE":
delete = MultiPartParser(request.META, BytesIO(request.body), request.upload_handlers, request.encoding).parse()
cid = delete[0]['cid']
try:
info = Course.objects.filter(cid=cid).get().to_dict()
Course.objects.filter(cid=cid).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()
cid = put[0]['cid']
c_name = put[0]['c_name']
c_type = put[0]['type']
credit = put[0]['credit']
tid = put[0]['tid']
Course.objects.filter(cid=cid).update(c_name=c_name, type=c_type, credit=credit, tid=tid)
data = Course.objects.filter(cid=cid)[0].to_dict()
return JsonResponse({'code': 200, 'msg': 'success', 'data': data}, safe=False)

Loading…
Cancel
Save