update code

scl-branch
bettleChen 1 year ago
parent e6551c7ddb
commit 7b09141c35

@ -1,8 +1,7 @@
import json import json
from django.shortcuts import render from django.shortcuts import render
from django.http import JsonResponse from django.http import JsonResponse, HttpResponse
# Create your views here. # Create your views here.
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
@ -22,7 +21,7 @@ def login(request):
if student: if student:
request.session["username"] = student.username request.session["username"] = student.username
request.session["type"] = "student" request.session["type"] = "student"
result = {"code": 0, "message": "login success!"} result = ResponseUtil.ok(student.to_dict(), "login success!")
else: else:
result = {"code": -1, "message": "username or password error!"} result = {"code": -1, "message": "username or password error!"}
elif _type == "teacher": elif _type == "teacher":
@ -31,12 +30,13 @@ def login(request):
if teacher: if teacher:
request.session["username"] = teacher.username request.session["username"] = teacher.username
request.session["type"] = "teacher" request.session["type"] = "teacher"
result = {"code": 0, "message": "login success!"} result = ResponseUtil.ok(teacher.to_dict(), "login success!")
else: else:
result = ResponseUtil.error("username or password error!") result = ResponseUtil.error("username or password error!")
else: else:
result = ResponseUtil.error("type error!") result = ResponseUtil.error("type error!")
return JsonResponse(result) return JsonResponse(result)
@csrf_exempt @csrf_exempt
def get_user_info(request): def get_user_info(request):
_type = request.GET.get("type") _type = request.GET.get("type")

@ -12,13 +12,7 @@ https://docs.djangoproject.com/en/2.2/ref/settings/
import os import os
CORS_ALLOW_ORIGIN_WHITELIST = [
"http://localhost:8080", # 允许访问的来源
"http://localhost:8000", # 允许访问的来源
# 可以继续添加其他允许的来源
]
CORS_ALLOW_CREDENTIALS = True # 允许跨域请求携带凭据例如Cookies
# Build paths inside the project like this: os.path.join(BASE_DIR, ...) # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@ -61,9 +55,27 @@ MIDDLEWARE = [
'django.contrib.messages.middleware.MessageMiddleware', 'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware', 'django.middleware.clickjacking.XFrameOptionsMiddleware',
'corsheaders.middleware.CorsMiddleware', 'corsheaders.middleware.CorsMiddleware',
'django.middleware.common.CommonMiddleware',
# 'API.middle.AuthMiddleware', # 'API.middle.AuthMiddleware',
] ]
CORS_ALLOW_CREDENTIALS = True
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_HEADERS = (
'XMLHttpRequest',
'X_FILENAME',
'accept-encoding',
'authorization',
'content-type',
'dnt',
'origin',
'user-agent',
'x-csrftoken',
'x-requested-with',
'Pragma',
)
ROOT_URLCONF = 'EduSystemServer.urls' ROOT_URLCONF = 'EduSystemServer.urls'

@ -13,7 +13,7 @@ class Course(models.Model):
tid = models.ForeignKey(Teacher, to_field="tid", on_delete=models.CASCADE, name="tid") tid = models.ForeignKey(Teacher, to_field="tid", on_delete=models.CASCADE, name="tid")
def to_dict(self): def to_dict(self):
return {"cid": self.cid, "c_name": self.c_name, "type": self.type, "credit": self.credit, return {"cid": self.cid, "c_name": self.name, "type": self.type, "credit": self.credit,
"tid": self.tid} "tid": self.tid}
class Meta: class Meta:

Loading…
Cancel
Save