diff --git a/pythonProject03/mysite10_7/mysite10_7/__pycache__/settings.cpython-37.pyc b/pythonProject03/mysite10_7/mysite10_7/__pycache__/settings.cpython-37.pyc
index 405ddbc..825fc92 100644
Binary files a/pythonProject03/mysite10_7/mysite10_7/__pycache__/settings.cpython-37.pyc and b/pythonProject03/mysite10_7/mysite10_7/__pycache__/settings.cpython-37.pyc differ
diff --git a/pythonProject03/mysite10_7/mysite10_7/settings.py b/pythonProject03/mysite10_7/mysite10_7/settings.py
index 3526ca3..bf61e74 100644
--- a/pythonProject03/mysite10_7/mysite10_7/settings.py
+++ b/pythonProject03/mysite10_7/mysite10_7/settings.py
@@ -40,6 +40,7 @@ INSTALLED_APPS = [
'polls',
'rest_framework',
'drf_spectacular',
+ 'corsheaders'
]
MIDDLEWARE = [
@@ -50,8 +51,15 @@ MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
+ 'corsheaders.middleware.CorsMiddleware',
+ 'django.middleware.common.CommonMiddleware',
+
+
]
+CORS_ORIGIN_ALLOW_ALL = True
+CORS_ALLOW_CREDENTIALS = True
+CORS_ALLOW_ALL_ORIGINS = True
ROOT_URLCONF = 'mysite10_7.urls'
TEMPLATES = [
@@ -133,4 +141,28 @@ DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
REST_FRAMEWORK = {
'DEFAULT_SCHEMA_CLASS': 'drf_spectacular.openapi.AutoSchema', # 使用 drf-spectacular 生成接口
-}
\ No newline at end of file
+}
+# -----------------------
+CORS_ALLOW_METHODS = (
+ 'DELETE',
+ 'GET',
+ 'OPTIONS',
+ 'PATCH',
+ 'POST',
+ 'PUT',
+ 'VIEW',
+)
+
+CORS_ALLOW_HEADERS = (
+ 'XMLHttpRequest',
+ 'X_FILENAME',
+ 'accept-encoding',
+ 'authorization',
+ 'content-type',
+ 'dnt',
+ 'origin',
+ 'user-agent',
+ 'x-csrftoken',
+ 'x-requested-with',
+ 'Pragma',
+)
\ No newline at end of file
diff --git a/pythonProject03/mysite10_7/polls/__pycache__/tests.cpython-37.pyc b/pythonProject03/mysite10_7/polls/__pycache__/tests.cpython-37.pyc
new file mode 100644
index 0000000..e943c84
Binary files /dev/null and b/pythonProject03/mysite10_7/polls/__pycache__/tests.cpython-37.pyc differ
diff --git a/pythonProject03/mysite10_7/polls/__pycache__/urls.cpython-37.pyc b/pythonProject03/mysite10_7/polls/__pycache__/urls.cpython-37.pyc
index c905de5..13eb7f4 100644
Binary files a/pythonProject03/mysite10_7/polls/__pycache__/urls.cpython-37.pyc and b/pythonProject03/mysite10_7/polls/__pycache__/urls.cpython-37.pyc differ
diff --git a/pythonProject03/mysite10_7/polls/__pycache__/views.cpython-37.pyc b/pythonProject03/mysite10_7/polls/__pycache__/views.cpython-37.pyc
index 567d620..e3a2258 100644
Binary files a/pythonProject03/mysite10_7/polls/__pycache__/views.cpython-37.pyc and b/pythonProject03/mysite10_7/polls/__pycache__/views.cpython-37.pyc differ
diff --git a/pythonProject03/mysite10_7/polls/templates/__pycache__/__init__.cpython-37.pyc b/pythonProject03/mysite10_7/polls/templates/__pycache__/__init__.cpython-37.pyc
new file mode 100644
index 0000000..bc3983e
Binary files /dev/null and b/pythonProject03/mysite10_7/polls/templates/__pycache__/__init__.cpython-37.pyc differ
diff --git a/pythonProject03/mysite10_7/polls/templates/confirm_roll_call.html b/pythonProject03/mysite10_7/polls/templates/confirm_roll_call.html
index 95eb141..5cc69ed 100644
--- a/pythonProject03/mysite10_7/polls/templates/confirm_roll_call.html
+++ b/pythonProject03/mysite10_7/polls/templates/confirm_roll_call.html
@@ -1,3 +1,38 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
@@ -31,45 +66,10 @@
+
+
+
+
-
-
-
-
- 确认点名
-
-
-
-
-{% if protection_awarded %}
-该学生获得保护权,自动加2分!
-{% endif %}
-
-
-
diff --git a/pythonProject03/mysite10_7/polls/templates/leaderboard.html b/pythonProject03/mysite10_7/polls/templates/leaderboard.html
index 997ce63..2b5da7a 100644
--- a/pythonProject03/mysite10_7/polls/templates/leaderboard.html
+++ b/pythonProject03/mysite10_7/polls/templates/leaderboard.html
@@ -1,27 +1,27 @@
-
-
-
- 积分排行榜
-
-
- 积分排行榜
-
-
- 排名 |
- 学号 |
- 姓名 |
- 积分 |
- 到课次数 |
-
- {% for student in students %}
-
- {{ forloop.counter }} |
- {{ student.student_id }} |
- {{ student.name }} |
- {{ student.score }} |
- {{ student.attendance_count }} |
-
- {% endfor %}
-
-
-
\ No newline at end of file
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/pythonProject03/mysite10_7/polls/templates/login.html b/pythonProject03/mysite10_7/polls/templates/login.html
index 4b3567f..0bb44a6 100644
--- a/pythonProject03/mysite10_7/polls/templates/login.html
+++ b/pythonProject03/mysite10_7/polls/templates/login.html
@@ -1,15 +1,15 @@
-
-
-
-
- 登录
-
-
-
-
-
\ No newline at end of file
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/pythonProject03/mysite10_7/polls/templates/register.html b/pythonProject03/mysite10_7/polls/templates/register.html
index bb353f2..1515a51 100644
--- a/pythonProject03/mysite10_7/polls/templates/register.html
+++ b/pythonProject03/mysite10_7/polls/templates/register.html
@@ -1,15 +1,15 @@
-
-
-
-
- 注册
-
-
-
-
-
\ No newline at end of file
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/pythonProject03/mysite10_7/polls/templates/upload_students.html b/pythonProject03/mysite10_7/polls/templates/upload_students.html
index 68bae50..d99ce94 100644
--- a/pythonProject03/mysite10_7/polls/templates/upload_students.html
+++ b/pythonProject03/mysite10_7/polls/templates/upload_students.html
@@ -1,15 +1,15 @@
-
-
-
-
- Title
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
-
-
\ No newline at end of file
+
+
\ No newline at end of file
diff --git a/pythonProject03/mysite10_7/polls/tests.py b/pythonProject03/mysite10_7/polls/tests.py
index 7ce503c..e69de29 100644
--- a/pythonProject03/mysite10_7/polls/tests.py
+++ b/pythonProject03/mysite10_7/polls/tests.py
@@ -1,3 +0,0 @@
-from django.test import TestCase
-
-# Create your tests here.
diff --git a/pythonProject03/mysite10_7/polls/views.py b/pythonProject03/mysite10_7/polls/views.py
index b1b155d..b3fff2b 100644
--- a/pythonProject03/mysite10_7/polls/views.py
+++ b/pythonProject03/mysite10_7/polls/views.py
@@ -1,165 +1,3 @@
-# from django.shortcuts import render
-# from django.http import HttpResponse
-# from .models import *
-# from django.shortcuts import render, redirect, get_object_or_404
-# import random
-# import pandas as pd
-# from django.db.models import Window, F
-# from django.db.models.functions import Rank
-# # 导入表单
-# from .forms import UploadFileForm
-#
-# from rest_framework import viewsets
-# from .serializers import StudentSerializer
-#
-# from rest_framework.response import Response
-# from rest_framework.decorators import api_view
-# # Create your views here.
-# def toLogin_view(request):
-# return render(request,'login.html')
-# def Login_view(request):
-# u=request.POST.get("user",'')
-# p=request.POST.get("pwd",'')
-#
-# if u and p:
-# c=StudentInfo.objects.filter(stu_name=u,stu_pwd=p).count()
-# if c >= 1:
-# return HttpResponse("登录成功!")
-# else:
-# return HttpResponse("账号密码错误!")
-# else:
-# return HttpResponse("请输入正确的账号和密码!")
-#
-# def toregister_view(request):
-# return render(request, 'register.html')
-#
-# # #点击注册后做的逻辑判断
-# def register_view(request):
-# u = request.POST.get("user", '')
-# p = request.POST.get("pwd", '')
-# if u and p:
-# stu = StudentInfo(stu_id=random.choice('0123456789'),stu_name=u, stu_pwd=p)
-# stu.save()
-# return HttpResponse("注册成功")
-# else:
-# return HttpResponse("请输入完整的账号和密码!")
-#
-# #导入excel
-# # 上传学生名单的视图
-# def upload_students(request):
-# if request.method == 'POST':
-# form = UploadFileForm(request.POST, request.FILES)
-# if form.is_valid():
-# # 读取上传的 Excel 文件
-# excel_file = request.FILES['file']
-# df = pd.read_excel(excel_file) # 使用 pandas 读取 Excel 文件
-#
-# # 遍历 DataFrame,将每个学生保存到数据库
-# for _, row in df.iterrows():
-# student_id = row['student_id']
-# name = row['name']
-# Student.objects.get_or_create(student_id=student_id, name=name) # 如果学生已存在则不创建
-#
-# return redirect('roll_call') # 完成后重定向到点名页面
-# else:
-# form = UploadFileForm()
-#
-# return render(request, 'upload_students.html', {'form': form}) # 渲染上传页面
-#
-# # 开始点名的视图
-# def roll_call(request):
-# students = Student.objects.all() # 获取所有学生
-# selected_student = None # 初始化被选中的学生
-#
-# # 当教师点击“开始点名”按钮时
-# if request.method == 'POST' and 'start_roll_call' in request.POST:
-# # 设置权重:总分越高,被点名的概率越低
-# weights = [1 / (student.score + 1) for student in students] # 根据分数调整被点名概率
-# selected_student = random.choices(students, weights=weights, k=1)[0] # 随机选择一个学生
-# request.session['selected_student_id'] = selected_student.student_id # 存储被点名学生的ID到session中
-# return redirect('confirm_roll_call') # 跳转到确认点名页面
-#
-# return render(request, 'roll_call.html', {'selected_student': selected_student}) # 渲染点名页面
-#
-# # 确认点名的视图
-# # def confirm_roll_call(request):
-# # # 从 session 中获取被点名的学生
-# # student_id = request.session.get('selected_student_id')
-# # student = get_object_or_404(Student, student_id=student_id)
-# #
-# # if request.method == 'POST':
-# # # 学生是否到课
-# # if 'attended' in request.POST: # 如果选择了到课
-# # student.score += 1 # 到课加1分
-# # student.attendance_count += 1 # 到课次数加1
-# #
-# # # 处理是否准确重复问题
-# # if request.POST['question_repeat'] == 'accurate':
-# # student.score += 0.5 # 重复问题准确,加0.5分
-# # else:
-# # student.score -= 1 # 重复问题不准确,扣1分
-# #
-# # # 处理回答问题的准确性(0-3分)
-# # answer_accuracy = float(request.POST.get('answer_accuracy', 0))
-# # student.score += answer_accuracy # 根据回答准确性加分
-# # else:
-# # student.score -= 5 # 未到课扣5分
-# #
-# # student.save() # 保存更新后的学生信息
-# # return redirect('roll_call') # 返回点名页面,进行下一轮点名
-# #
-# # return render(request, 'confirm_roll_call.html', {'student': student}) # 渲染确认点名页面
-# def confirm_roll_call(request):
-# student_id = request.session.get('selected_student_id')
-# student = get_object_or_404(Student, student_id=student_id)
-# protection_awarded = False # 初始化保护权标志
-#
-# if request.method == 'POST':
-# # 增加学生的被点名次数
-# student.called_count += 1
-#
-# # 检查学生是否有“保护权”
-# if student.called_count % 3 == 0:
-# # 如果被点名次数是3的倍数,赋予保护权,自动加2分
-# student.score += 2
-# protection_awarded = True # 设置保护权标志
-# print(f"Student {student.name} ({student.student_id})获得保护权,自动加2分")
-# else:
-# # 学生是否到课
-# if 'attended' in request.POST: # 如果选择了到课
-# student.score += 1 # 到课加1分
-# student.attendance_count += 1 # 到课次数加1
-#
-# # 处理是否准确重复问题
-# if request.POST['question_repeat'] == 'accurate':
-# student.score += 0.5 # 重复问题准确,加0.5分
-# else:
-# student.score -= 1 # 重复问题不准确,扣1分
-#
-# # 处理回答问题的准确性(0-3分)
-# answer_accuracy = float(request.POST.get('answer_accuracy', 0))
-# student.score += answer_accuracy # 根据回答准确性加分
-# else:
-# student.score -= 5 # 未到课扣5分
-#
-# # 保存更新后的学生信息
-# student.save()
-# return redirect('roll_call') # 返回点名页面,进行下一轮点名
-#
-# return render(request, 'confirm_roll_call.html', {'student': student, 'protection_awarded': protection_awarded}) # 渲染确认点名页面
-#
-#
-#
-#
-# # 积分排行榜
-# def leaderboard(request):
-# students = Student.objects.all().order_by('-score') # 按分数降序排列
-# return render(request, 'leaderboard.html', {'students': students})
-#
-#
-# class StudentViewSet(viewsets.ModelViewSet):
-# queryset = Student.objects.all() # 获取所有学生数据
-# serializer_class = StudentSerializer # 指定序列化器
from django.shortcuts import render
from django.http import HttpResponse
from .models import *
@@ -167,86 +5,43 @@ from django.shortcuts import render, redirect, get_object_or_404
import random
from django.http import JsonResponse
import pandas as pd
+import json
from django.db.models import Window, F
from django.db.models.functions import Rank
from django.shortcuts import get_object_or_404
+from django.views.decorators.csrf import csrf_exempt
# 导入表单
from .forms import UploadFileForm
-# Create your views here.
-def toLogin_view(request):
- return render(request,'login.html')
-# def Login_view(request):
-# u=request.POST.get("user",'')
-# p=request.POST.get("pwd",'')
-#
-# if u and p:
-# c=StudentInfo.objects.filter(stu_name=u,stu_pwd=p).count()
-# if c >= 1:
-# return HttpResponse("登录成功!")
-# else:
-# return HttpResponse("账号密码错误!")
-# else:
-# return HttpResponse("请输入正确的账号和密码!")
-def Login_view(request):
- u = request.POST.get("user", '')
- p = request.POST.get("pwd", '')
-
- if u and p:
- c = StudentInfo.objects.filter(stu_name=u, stu_pwd=p).count()
- if c >= 1:
- return JsonResponse({"message": "登录成功!"})
- else:
- return JsonResponse({"message": "账号密码错误!"}, status=400)
- else:
- return JsonResponse({"message": "请输入正确的账号和密码!"}, status=400)
+@csrf_exempt
def toregister_view(request):
- return render(request, 'register.html')
+ return JsonResponse({'message': '注册成功!'})
+import json
+from django.views.decorators.csrf import csrf_exempt
- # #点击注册后做的逻辑判断
-# def register_view(request):
-# u = request.POST.get("user", '')
-# p = request.POST.get("pwd", '')
-# if u and p:
-# stu = StudentInfo(stu_id=random.choice('0123456789'),stu_name=u, stu_pwd=p)
-# stu.save()
-# return HttpResponse("注册成功")
-# else:
-# return HttpResponse("请输入完整的账号和密码!")
+@csrf_exempt # 可选,视具体情况决定是否使用
+def register_view(request):
+ if request.method == 'POST':
+ try:
+ data = json.loads(request.body) # 从请求体中加载 JSON 数据
+ u = data.get("user", '')
+ p = data.get("pwd", '')
+
+ if u and p:
+ stu = StudentInfo(stu_id=random.choice('0123456789'), stu_name=u, stu_pwd=p)
+ stu.save()
+ return JsonResponse({"message": "注册成功"})
+ else:
+ return JsonResponse({"message": "请输入完整的账号和密码!"}, status=400)
+ except json.JSONDecodeError:
+ return JsonResponse({"message": "无效的 JSON 数据!"}, status=400)
+
+ return JsonResponse({"message": "只支持 POST 请求!"}, status=405)
-def register_view(request):
- u = request.POST.get("user", '')
- p = request.POST.get("pwd", '')
- if u and p:
- stu = StudentInfo(stu_id=random.choice('0123456789'), stu_name=u, stu_pwd=p)
- stu.save()
- return JsonResponse({"message": "注册成功"})
- else:
- return JsonResponse({"message": "请输入完整的账号和密码!"}, status=400)
-
-#导入excel
-# 上传学生名单的视图
-# def upload_students(request):
-# if request.method == 'POST':
-# form = UploadFileForm(request.POST, request.FILES)
-# if form.is_valid():
-# # 读取上传的 Excel 文件
-# excel_file = request.FILES['file']
-# df = pd.read_excel(excel_file) # 使用 pandas 读取 Excel 文件
-#
-# # 遍历 DataFrame,将每个学生保存到数据库
-# for _, row in df.iterrows():
-# student_id = row['student_id']
-# name = row['name']
-# Student.objects.get_or_create(student_id=student_id, name=name) # 如果学生已存在则不创建
-#
-# return redirect('roll_call') # 完成后重定向到点名页面
-# else:
-# form = UploadFileForm()
#
-# return render(request, 'upload_students.html', {'form': form}) # 渲染上传页面
+@csrf_exempt
def upload_students(request):
if request.method == 'POST':
form = UploadFileForm(request.POST, request.FILES)
@@ -268,156 +63,116 @@ def upload_students(request):
return render(request, 'upload_students.html', {'form': form}) # 确保在GET请求时返回HttpResponse
-# return render(request, 'upload_students.html', {'form': form}) # 渲染上传页面
-# # 开始点名的视图
-# def roll_call(request):
-# students = Student.objects.all() # 获取所有学生
-# selected_student = None # 初始化被选中的学生
-#
-# # 当教师点击“开始点名”按钮时
-# if request.method == 'POST' and 'start_roll_call' in request.POST:
-# # 设置权重:总分越高,被点名的概率越低
-# weights = [1 / (student.score + 1) for student in students] # 根据分数调整被点名概率
-# selected_student = random.choices(students, weights=weights, k=1)[0] # 随机选择一个学生
-# request.session['selected_student_id'] = selected_student.student_id # 存储被点名学生的ID到session中
-# return redirect('confirm_roll_call') # 跳转到确认点名页面
-#
-# return render(request, 'roll_call.html', {'selected_student': selected_student}) # 渲染点名页面
+
+@csrf_exempt # 如果需要,可以移除此装饰器,确保 CSRF 保护
def roll_call(request):
students = Student.objects.all() # 获取所有学生
- selected_student = None # 初始化被选中的学生
# 当教师点击“开始点名”按钮时
- if request.method == 'POST' and 'start_roll_call' in request.POST:
- # 设置权重:总分越高,被点名的概率越低
- weights = [1 / (student.score + 1) for student in students] # 根据分数调整被点名概率
- selected_student = random.choices(students, weights=weights, k=1)[0] # 随机选择一个学生
- request.session['selected_student_id'] = selected_student.student_id # 存储被点名学生的ID到session中
- return JsonResponse({"message": "点名成功", "student_id": selected_student.student_id, "student_name": selected_student.name})
-
- return render(request, 'roll_call.html', {'selected_student': selected_student}) # 渲染点名页面
-# 确认点名的视图
-# def confirm_roll_call(request):
-# # 从 session 中获取被点名的学生
-# student_id = request.session.get('selected_student_id')
-# student = get_object_or_404(Student, student_id=student_id)
-#
-# if request.method == 'POST':
-# # 学生是否到课
-# if 'attended' in request.POST: # 如果选择了到课
-# student.score += 1 # 到课加1分
-# student.attendance_count += 1 # 到课次数加1
-#
-# # 处理是否准确重复问题
-# if request.POST['question_repeat'] == 'accurate':
-# student.score += 0.5 # 重复问题准确,加0.5分
-# else:
-# student.score -= 1 # 重复问题不准确,扣1分
-#
-# # 处理回答问题的准确性(0-3分)
-# answer_accuracy = float(request.POST.get('answer_accuracy', 0))
-# student.score += answer_accuracy # 根据回答准确性加分
-# else:
-# student.score -= 5 # 未到课扣5分
-#
-# student.save() # 保存更新后的学生信息
-# return redirect('roll_call') # 返回点名页面,进行下一轮点名
-#
-# return render(request, 'confirm_roll_call.html', {'student': student}) # 渲染确认点名页面
-# def confirm_roll_call(request):
-# student_id = request.session.get('selected_student_id')
-# student = get_object_or_404(Student, student_id=student_id)
-# protection_awarded = False # 初始化保护权标志
-#
-# if request.method == 'POST':
-# # 增加学生的被点名次数
-# student.called_count += 1
-#
-# # 检查学生是否有“保护权”
-# if student.called_count % 3 == 0:
-# # 如果被点名次数是3的倍数,赋予保护权,自动加2分
-# student.score += 2
-# protection_awarded = True # 设置保护权标志
-# print(f"Student {student.name} ({student.student_id})获得保护权,自动加2分")
-# else:
-# # 学生是否到课
-# if 'attended' in request.POST: # 如果选择了到课
-# student.score += 1 # 到课加1分
-# student.attendance_count += 1 # 到课次数加1
-#
-# # 处理是否准确重复问题
-# if request.POST['question_repeat'] == 'accurate':
-# student.score += 0.5 # 重复问题准确,加0.5分
-# else:
-# student.score -= 1 # 重复问题不准确,扣1分
-#
-# # 处理回答问题的准确性(0-3分)
-# answer_accuracy = float(request.POST.get('answer_accuracy', 0))
-# student.score += answer_accuracy # 根据回答准确性加分
-# else:
-# student.score -= 5 # 未到课扣5分
-#
-# # 保存更新后的学生信息
-# student.save()
-# return redirect('roll_call') # 返回点名页面,进行下一轮点名
-#
-# return render(request, 'confirm_roll_call.html', {'student': student, 'protection_awarded': protection_awarded}) # 渲染确认点名页面
-#
-#
-#
-#
-# # 积分排行榜
-# def leaderboard(request):
-# students = Student.objects.all().order_by('-score') # 按分数降序排列
-# return render(request, 'leaderboard.html', {'students': students})
+ if request.method == 'POST':
+ try:
+ data = json.loads(request.body) # 从请求中读取 JSON 数据
+ if data.get('start_roll_call') == 'true':
+ # 设置权重:总分越高,被点名的概率越低
+ weights = [1 / (student.score + 1) for student in students] # 根据分数调整被点名概率
+ selected_student = random.choices(students, weights=weights, k=1)[0] # 随机选择一个学生
+ request.session['selected_student_id'] = selected_student.student_id # 存储被点名学生的ID到session中
+ return JsonResponse({"message": "点名成功", "student_id": selected_student.student_id, "student_name": selected_student.name})
+ except json.JSONDecodeError:
+ return JsonResponse({"error": "无效的 JSON 数据"}, status=400)
+
+ # 返回错误信息,如果不是 POST 请求或没有触发点名
+ return JsonResponse({"error": "只支持 POST 请求"}, status=405)
+
+@csrf_exempt
def confirm_roll_call(request):
- student_id = request.session.get('selected_student_id')
- student = get_object_or_404(Student, student_id=student_id)
- protection_awarded = False # 初始化保护权标志
-
if request.method == 'POST':
- # 增加学生的被点名次数
- student.called_count += 1
-
- # 检查学生是否有“保护权”
- if student.called_count % 3 == 0:
- student.score += 2
- protection_awarded = True # 设置保护权标志
- print(f"Student {student.name} ({student.student_id})获得保护权,自动加2分")
- else:
- # 学生是否到课
- if 'attended' in request.POST: # 如果选择了到课
- student.score += 1 # 到课加1分
- student.attendance_count += 1 # 到课次数加1
-
- # 处理是否准确重复问题
- if request.POST['question_repeat'] == 'accurate':
- student.score += 0.5 # 重复问题准确,加0.5分
- else:
- student.score -= 1 # 重复问题不准确,扣1分
-
- # 处理回答问题的准确性(0-3分)
- answer_accuracy = float(request.POST.get('answer_accuracy', 0))
- student.score += answer_accuracy # 根据回答准确性加分
+ try:
+ # 从请求体中解析 JSON 数据
+ data = json.loads(request.body)
+ student_id = data.get('student_id')
+
+ # 确保 student_id 存在
+ if not student_id:
+ return JsonResponse({"message": "未找到被点名学生ID"}, status=400)
+
+ # 获取学生对象,若不存在则返回404
+ student = get_object_or_404(Student, student_id=student_id)
+ protection_awarded = False # 初始化保护权标志
+
+ # 增加学生的被点名次数
+ student.called_count += 1
+
+ # 检查学生是否有“保护权”
+ if student.called_count % 3 == 0:
+ student.score += 2
+ protection_awarded = True # 设置保护权标志
+ print(f"Student {student.name} ({student.student_id})获得保护权,自动加2分")
else:
- student.score -= 5 # 未到课扣5分
+ # 学生是否到课
+ attended = data.get('attended', 'false').lower() == 'true' # 转为布尔值
+ if attended: # 如果选择了到课
+ student.score += 1 # 到课加1分
+ student.attendance_count += 1 # 到课次数加1
+
+ # 处理是否准确重复问题
+ if data.get('question_repeat') == 'accurate':
+ student.score += 0.5 # 重复问题准确,加0.5分
+ else:
+ student.score -= 1 # 重复问题不准确,扣1分
+
+ # 处理回答问题的准确性(0-3分)
+ answer_accuracy = float(data.get('answer_accuracy', 0))
+ student.score += answer_accuracy # 根据回答准确性加分
+ else:
+ student.score -= 5 # 未到课扣5分
- # 保存更新后的学生信息
- student.save()
+ # 保存更新后的学生信息
+ student.save()
- return JsonResponse({
- "message": "确认点名成功",
- "student_id": student.student_id,
- "student_name": student.name,
- "current_score": student.score,
- "protection_awarded": protection_awarded
- })
+ return JsonResponse({
+ "message": "确认点名成功",
+ "student_id": student.student_id,
+ "student_name": student.name,
+ "current_score": student.score,
+ "protection_awarded": protection_awarded
+ })
- return render(request, 'confirm_roll_call.html',
- {'student': student, 'protection_awarded': protection_awarded}) # 渲染确认点名页面
+ except json.JSONDecodeError:
+ return JsonResponse({"message": "请求体解析错误"}, status=400)
+ # GET 请求时渲染页面
+ return JsonResponse({"message": "不支持的请求方法"}, status=405)
+@csrf_exempt
def leaderboard(request):
students = Student.objects.all().order_by('-score') # 按分数降序排列
leaderboard_data = [{"student_id": student.student_id, "name": student.name, "score": student.score} for student in students]
return JsonResponse({"leaderboard": leaderboard_data}) # 返回排行榜数据
+
+# 111
+
+
+@csrf_exempt # If you're testing without CSRF protection
+def toLogin_view(request):
+ return JsonResponse({"message": "请提供用户名和密码进行登录"})
+
+@csrf_exempt
+def Login_view(request):
+ if request.method == 'POST':
+ try:
+ data = json.loads(request.body)
+ u = data.get("user", '')
+ p = data.get("pwd", '')
+
+ if u and p:
+ c = StudentInfo.objects.filter(stu_name=u, stu_pwd=p).count()
+ if c >= 1:
+ return JsonResponse({"message": "登录成功!"})
+ else:
+ return JsonResponse({"message": "账号密码错误!"}, status=400)
+ else:
+ return JsonResponse({"message": "请输入正确的账号和密码!"}, status=400)
+ except json.JSONDecodeError:
+ return JsonResponse({"message": "无效的JSON数据!"}, status=400)
+ return JsonResponse({"message": "仅支持POST请求"}, status=405)
\ No newline at end of file
diff --git a/pythonProject03/mysite10_7/test/__init__.py b/pythonProject03/mysite10_7/test/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/pythonProject03/mysite10_7/test/__pycache__/__init__.cpython-37.pyc b/pythonProject03/mysite10_7/test/__pycache__/__init__.cpython-37.pyc
new file mode 100644
index 0000000..7c6c00d
Binary files /dev/null and b/pythonProject03/mysite10_7/test/__pycache__/__init__.cpython-37.pyc differ
diff --git a/pythonProject03/mysite10_7/test/test01.py b/pythonProject03/mysite10_7/test/test01.py
new file mode 100644
index 0000000..84ea577
--- /dev/null
+++ b/pythonProject03/mysite10_7/test/test01.py
@@ -0,0 +1,6 @@
+from django.http import JsonResponse
+
+def leaderboard_view(request):
+ response = JsonResponse({'data': 'your data'})
+ response["Access-Control-Allow-Origin"] = "http://localhost:8000"
+ return response
diff --git a/pythonProject03/mysite10_7/test/test_Login_view.py b/pythonProject03/mysite10_7/test/test_Login_view.py
new file mode 100644
index 0000000..e64a06a
--- /dev/null
+++ b/pythonProject03/mysite10_7/test/test_Login_view.py
@@ -0,0 +1,19 @@
+import requests
+
+# 目标 URL
+url = 'http://127.0.0.1:8000/polls/index/'
+
+# 测试数据
+data = {
+ 'user': 'zmm', # 用于测试的用户名
+ 'pwd': 'Zmm' # 用于测试的密码
+}
+
+# 发送 POST 请求
+response = requests.post(url, data=data)
+
+# 检查响应状态和内容
+if response.status_code == 200:
+ print("登录成功:", response.json())
+else:
+ print("登录失败:", response.status_code, response.json())
diff --git a/pythonProject03/mysite10_7/test/test_confirm_roll_call.py b/pythonProject03/mysite10_7/test/test_confirm_roll_call.py
new file mode 100644
index 0000000..a112bf6
--- /dev/null
+++ b/pythonProject03/mysite10_7/test/test_confirm_roll_call.py
@@ -0,0 +1,29 @@
+import requests
+import json
+
+# 点名请求
+roll_call_url = 'http://127.0.0.1:8000/polls/roll_call/'
+roll_call_response = requests.post(roll_call_url, json={'start_roll_call': 'true'})
+
+if roll_call_response.status_code == 200:
+ print("点名成功:", roll_call_response.json())
+
+ selected_student_id = roll_call_response.json()['student_id']
+
+ # 确认点名请求
+ confirm_roll_call_url = 'http://127.0.0.1:8000/polls/confirm_roll_call/'
+ confirm_data = {
+ 'student_id': selected_student_id,
+ 'attended': 'true',
+ 'question_repeat': 'accurate',
+ 'answer_accuracy': 2.0
+ }
+
+ confirm_response = requests.post(confirm_roll_call_url, json=confirm_data)
+
+ if confirm_response.status_code == 200:
+ print("确认点名成功:", confirm_response.json())
+ else:
+ print("确认点名失败:", confirm_response.status_code, confirm_response.text)
+else:
+ print("点名失败:", roll_call_response.status_code, roll_call_response.text)
diff --git a/pythonProject03/mysite10_7/test/test_leaderboard.py b/pythonProject03/mysite10_7/test/test_leaderboard.py
new file mode 100644
index 0000000..1e73a6a
--- /dev/null
+++ b/pythonProject03/mysite10_7/test/test_leaderboard.py
@@ -0,0 +1,13 @@
+import requests
+
+# 设定接口 URL
+leaderboard_url = 'http://127.0.0.1:8000/polls/leaderboard/'
+
+# 发送 GET 请求
+response = requests.get(leaderboard_url)
+
+# 检查请求是否成功
+if response.status_code == 200:
+ print("排行榜数据:", response.json())
+else:
+ print("请求失败:", response.status_code, response.text)
diff --git a/pythonProject03/mysite10_7/test/test_register.py b/pythonProject03/mysite10_7/test/test_register.py
new file mode 100644
index 0000000..43e0a72
--- /dev/null
+++ b/pythonProject03/mysite10_7/test/test_register.py
@@ -0,0 +1,19 @@
+import requests
+import json
+
+# 目标 URL
+url = 'http://127.0.0.1:8000/polls/register/'
+
+# 发送 POST 请求,使用 json 参数而不是 data
+data = {
+ 'user': 'zmm12300000', # 用于测试的用户名
+ 'pwd': 'Zmm12314' # 用于测试的密码
+}
+
+response = requests.post(url, json=data) # 使用 json 参数发送数据
+
+# 检查响应状态和内容
+if response.status_code == 200:
+ print("注册成功:", response.json())
+else:
+ print("注册失败:", response.status_code, response.json())
diff --git a/pythonProject03/mysite10_7/test/test_roll_call.py b/pythonProject03/mysite10_7/test/test_roll_call.py
new file mode 100644
index 0000000..7cb7513
--- /dev/null
+++ b/pythonProject03/mysite10_7/test/test_roll_call.py
@@ -0,0 +1,20 @@
+import requests
+
+# 目标 URL
+url = 'http://127.0.0.1:8000/polls/roll_call/'
+
+# 发送 POST 请求,模拟教师点击“开始点名”按钮
+data = {
+ 'start_roll_call': 'true' # 发送一个模拟参数以触发点名功能
+}
+
+response = requests.post(url, json=data) # 使用 json 参数
+
+# 检查响应状态和内容
+if response.status_code == 200:
+ try:
+ print("点名成功:", response.json())
+ except ValueError:
+ print("响应不是有效的 JSON:", response.text)
+else:
+ print("点名失败:", response.status_code, response.text)
diff --git a/pythonProject03/mysite10_7/test/tset_upload_students.py b/pythonProject03/mysite10_7/test/tset_upload_students.py
new file mode 100644
index 0000000..aade882
--- /dev/null
+++ b/pythonProject03/mysite10_7/test/tset_upload_students.py
@@ -0,0 +1,18 @@
+import requests
+
+# 目标 URL
+url = 'http://127.0.0.1:8000/polls/upload/' # 确保与 Django 开发服务器地址一致
+
+# 准备要上传的 Excel 文件
+files = {
+ 'file': open('D:/python20240709/pycharm/mingdan.xlsx', 'rb') # 替换为你的 Excel 文件路径
+}
+
+# 发送 POST 请求
+response = requests.post(url, files=files)
+
+# 检查响应状态和内容
+if response.status_code == 200:
+ print("学生信息上传成功:", response.json())
+else:
+ print("上传失败:", response.status_code, response.json())