数据管理页面的完善
This commit is contained in:
@@ -1,75 +0,0 @@
|
||||
<!DOCTYPE html>
|
||||
<html lang="zh-CN">
|
||||
<head>
|
||||
<meta charset="UTF-8">
|
||||
<title>填写班级信息</title>
|
||||
<style>
|
||||
body { font-family: system-ui, -apple-system, Segoe UI, Roboto, sans-serif; background: #f5f6fa; }
|
||||
.container { max-width: 400px; margin: 15vh auto; padding: 24px; background: #fff; border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,0.08); }
|
||||
h1 { font-size: 20px; margin: 0 0 16px; text-align: center; }
|
||||
label { display:block; margin: 12px 0 6px; color:#333; }
|
||||
input { width:100%; padding:10px 0px; border:1px solid #dcdde1; border-radius:6px; box-sizing: border-box; }
|
||||
button { width:100%; margin-top:16px; padding:10px 12px; background:#2d8cf0; color:#fff; border:none; border-radius:6px; cursor:pointer; }
|
||||
button:disabled { background:#9bbcf0; cursor:not-allowed; }
|
||||
.error { color:#d93025; margin-top:10px; min-height:20px; font-size: 14px; }
|
||||
.hint { color:#888; font-size:12px; margin-top:10px; }
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>完善班级信息</h1>
|
||||
<form id="classForm">
|
||||
{% csrf_token %}
|
||||
<input type="hidden" id="user_id" value="{{ user_id }}">
|
||||
<label for="class_name">所在班级</label>
|
||||
<input id="class_name" name="class_name" type="text" placeholder="例:2024级计算机专业1班" required />
|
||||
<div class="hint">格式要求:XXXX级YY专业Z班(记得加“专业”两个字)</div>
|
||||
<button id="submitBtn" type="submit">提交并进入登录页</button>
|
||||
<div id="error" class="error"></div>
|
||||
</form>
|
||||
</div>
|
||||
<script>
|
||||
function getCookie(name){const v=`; ${document.cookie}`;const p=v.split(`; ${name}=`);if(p.length===2) return p.pop().split(';').shift();}
|
||||
|
||||
document.getElementById('classForm').addEventListener('submit', async (e) => {
|
||||
e.preventDefault();
|
||||
const err = document.getElementById('error');
|
||||
err.textContent = '';
|
||||
const className = document.getElementById('class_name').value.trim();
|
||||
const userId = document.getElementById('user_id').value;
|
||||
|
||||
// 正则校验:2024级**专业*班
|
||||
const pattern = /^\d{4}级.+专业\d+班$/;
|
||||
if (!pattern.test(className)) {
|
||||
err.textContent = '班级格式不正确,请重新输入(例:2024级计算机专业1班)';
|
||||
return;
|
||||
}
|
||||
|
||||
const btn = document.getElementById('submitBtn');
|
||||
btn.disabled = true;
|
||||
|
||||
try {
|
||||
const csrftoken = getCookie('csrftoken');
|
||||
const resp = await fetch('/accounts/class-info/submit/', {
|
||||
method: 'POST',
|
||||
credentials: 'same-origin',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'X-CSRFToken': csrftoken || ''
|
||||
},
|
||||
body: JSON.stringify({ user_id: userId, class_name: className })
|
||||
});
|
||||
const data = await resp.json();
|
||||
if (!resp.ok || !data.ok) {
|
||||
throw new Error(data.message || '提交失败');
|
||||
}
|
||||
window.location.href = '/accounts/login/';
|
||||
} catch (e) {
|
||||
err.textContent = e.message || '发生错误';
|
||||
} finally {
|
||||
btn.disabled = false;
|
||||
}
|
||||
});
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -12,7 +12,5 @@ urlpatterns = [
|
||||
path("register/", views.register_page, name="register"),
|
||||
path("register/submit/", views.register_submit, name="register_submit"),
|
||||
path("email/send-code/", views.send_email_code, name="send_email_code"),
|
||||
path("class-info/", views.class_info_page, name="class_info"),
|
||||
path("class-info/submit/", views.class_info_submit, name="class_info_submit"),
|
||||
path("profile/", views.profile_page, name="profile"),
|
||||
]
|
||||
@@ -272,43 +272,7 @@ def register_submit(request):
|
||||
del request.session["email_verify"]
|
||||
except Exception:
|
||||
pass
|
||||
# 修改:注册成功后跳转到完善班级信息页面
|
||||
return JsonResponse({"ok": True, "redirect_url": f"/accounts/class-info/?user_id={next_id}"})
|
||||
|
||||
@require_http_methods(["GET"])
|
||||
@ensure_csrf_cookie
|
||||
def class_info_page(request):
|
||||
user_id = request.GET.get("user_id")
|
||||
if not user_id:
|
||||
return redirect("/accounts/register/")
|
||||
return render(request, "accounts/class_info.html", {"user_id": user_id})
|
||||
|
||||
@require_http_methods(["POST"])
|
||||
@csrf_protect
|
||||
def class_info_submit(request):
|
||||
try:
|
||||
payload = json.loads(request.body.decode("utf-8"))
|
||||
except json.JSONDecodeError:
|
||||
return HttpResponseBadRequest("Invalid JSON")
|
||||
|
||||
user_id = payload.get("user_id")
|
||||
class_name = (payload.get("class_name") or "").strip()
|
||||
|
||||
if not user_id or not class_name:
|
||||
return HttpResponseBadRequest("Missing fields")
|
||||
|
||||
# 后端校验:2024级**专业*班
|
||||
import re
|
||||
pattern = r"^\d{4}级.+专业\d+班$"
|
||||
if not re.match(pattern, class_name):
|
||||
return JsonResponse({"ok": False, "message": "班级格式不正确"}, status=400)
|
||||
|
||||
# 更新用户信息,将班级信息存入 key 列表
|
||||
ok = update_user_by_id(user_id, key=[class_name])
|
||||
if not ok:
|
||||
return JsonResponse({"ok": False, "message": "保存班级信息失败"}, status=500)
|
||||
|
||||
return JsonResponse({"ok": True})
|
||||
return JsonResponse({"ok": True, "redirect_url": "/accounts/login/"})
|
||||
|
||||
@require_http_methods(["POST"])
|
||||
@csrf_protect
|
||||
|
||||
Reference in New Issue
Block a user