Django 小实例S1 简易学生选课管理系统 6 实现登录逻辑
Django 小实例S1 简易学生选课管理系统 第6节——实现登录逻辑
点击查看教程总目录
作者自我介绍:b站小UP主,时常直播编程+红警三,python1对1辅导老师。
1 业务逻辑
本教程第四节里面实现了登录页面和一个空的登录逻辑。
第六节这里就把登录逻辑补全。
登录的业务逻辑是:
- 检查是否注册
- 未注册,则提示账号不存在
- 注册但密码不匹配,提示密码不正确
- 注册且信息匹配,成功登录,跳转到个人主页,同时通过cookie保存登录信息。
对于教务管理系统,个人主页应该是其课程主页,所以本部分还需要添加课程主页,这里先只实现一个空的展示其个人信息的课程主页。
2 修改登录视图
修改user/views.py
中的login
方法如下:
def login(request, kind):
if kind not in ["teacher", "student"]:
return HttpResponse(INVALID_KIND)
if request.method == 'POST':
if kind == "teacher":
form = TeaLoginForm(data=request.POST)
else:
form = StuLoginForm(data=request.POST)
if form.is_valid():
uid = form.cleaned_data["uid"]
if len(uid) != 10:
form.add_error("uid", "账号长度必须为10")
else:
if kind == "teacher":
department_no = uid[:3]
number = uid[3:]
object_set = Teacher.objects.filter(department_no=department_no, number=number)
else:
grade = uid[:4]
number = uid[4:]
object_set = Student.objects.filter(grade=grade, number=number)
if object_set.count() == 0:
form.add_error("uid", "该账号不存在.")
else:
user = object_set[0]
if form.cleaned_data["password"] != user.password:
form.add_error("password", "密码不正确.")
else:
request.session['kind'] = kind
request.session['user'] = uid
request.session['id'] = user.id
return redirect("course", kind=kind)
return render(request, 'user/login_detail.html', {'form': form, 'kind': kind})
else:
context = {'kind': kind}
if request.GET.get('uid'):
uid = request.GET.get('uid')
context['uid'] = uid
if kind == "teacher":
form = TeaLoginForm({"uid": uid, 'password': '12345678'})
else:
form = StuLoginForm({"uid": uid, 'password': '12345678'})
else:
if kind == "teacher":
form = TeaLoginForm()
else:
form = StuLoginForm()
context['form'] = form
if request.GET.get('from_url'):
context['from_url'] = request.GET.get('from_url')
return render(request, 'user/login_detail.html', context)
登录后会在cookie中存储以下信息:
kind
: 用户类型,学生或老师user
: 用户学号或教师编号
3 添加简单主页
目前虽然是个人主页,但是后面将会是课程主页,老师的和学生的必然是不同的。
所以我们现在也先提前规划好,将主页分为学生的和老师的来处理。
course/views.py
代码如下
from django.http.response import HttpResponse
from django.shortcuts import render, reverse, redirect
from user.models import Student, Teacher
from constants import INVALID_KIND
def get_user(request, kind):
"""
:param request:
:param kind: teacher or student
:return: return Teacher instance or Student instance
"""
if request.session.get('kind', '') != kind or kind not in ["student", "teacher"]:
return None
if len(request.session.get('user', '')) != 10:
return None
uid = request.session.get('user')
if kind == "student":
# 找到对应学生
grade = uid[:4]
number = uid[4:]
student_set = Student.objects.filter(grade=grade, number=number)
if student_set.count() == 0:
return None
return student_set[0]
else:
# 找到对应老师
department_no = uid[:3]
number = uid[3:]
teacher_set = Teacher.objects.filter(department_no=department_no, number=number)
if teacher_set.count() == 0:
return None
return teacher_set[0]
# Create your views here.
def home(request, kind):
if kind == "teacher":
return teacher_home(request)
elif kind == "student":
return student_home(request)
return HttpResponse(INVALID_KIND)
def teacher_home(request):
kind = "teacher"
user = get_user(request, kind)
if not user:
return redirect('login', kind=kind)
info = {
"name": user.name,
"kind": kind
}
context = {
"info": info
}
return render(request, 'course/nav.html', context)
def student_home(request):
kind = "student"
user = get_user(request, kind)
if not user:
return redirect('login', kind = kind)
info = {
"name": user.name,
"kind": kind
}
context = {
"info": info
}
return render(request, 'course/nav.html', context)
注意,跳转到一个带参数的url,有两种写法
return redirect(reverse("login", kwargs={"kind": kind}))
return redirect('login', kind = kind)
这两种写法返回效果一样
添加模板文件templates/course/nav.html
<!DOCTYPE html>
<html lang="en">
{% load static %}
<head>
<meta charset="UTF-8">
<title>
{% block title %}{% endblock %}
</title>
</head>
<body>
<div class="nav">
<div class="nav-title">
<a href="{% url 'course' kind=info.kind %}">
<p class="main-title">学生选课管理系统</p>
<p class="sub-title">
{% if info.kind == "teacher" %}
教师端
{% elif info.kind == "student" %}
学生端
{% endif %}</p>
</a>
</div>
<div class="name-logo">
<div class="user-name">
{{ info.name }}
</div>
</div>
</div>
<div class="main-content">
{% block content %}{% endblock %}
</div>
</body>
</html>
然后添加course/urls.py
如下
from django.urls import path
from course.views import *
urlpatterns = [
path('<slug:kind>/', home, name="course"),
]
同时还要去改下主url,即在SSCMS/urls.py
的urlpatterns
中添加
path('course/', include("course.urls")),
此时运行软件,登录账号后(这里又注册了一个叫李大爽的用户),
结果如图
4 实现退出登录
退出登录的视图方法如下(在user/views.py
中添加)
def logout(request):
if request.session.get("kind", ""):
del request.session["kind"]
if request.session.get("user", ""):
del request.session["user"]
if request.session.get("id", ""):
del request.session["id"]
return redirect(reverse("login"))
添加对应路由(在user/urls.py
中的urlpatterns
的添加)
path('logout/', views.logout, name="logout")
在修改下登录后的主页视图(即templates/course/nav.html
)
在其中的第24行处(即<div class="name-logo">
的之前)添加
<div class="log-out">
<a href="{% url "logout" %}">退出</a>
</div>
此时登录后,主页面如下图
点击退出按钮,就可以退出登录,返回登录主页面。
5 简化登录主页url
每次要打开登录主页,都需要在浏览器中输入http://127.0.0.1:8000/user/login
运行django后,控制台只自动给出http://127.0.0.1:8000
。
所以这里修改下urlpattern,能够访问http://127.0.0.1:8000
即得到登录主页
修改后的SSCMS/urls.py
如下
from django.urls import path, include
from user.views import home
urlpatterns = [
path('', home, name="login"),
path('user/', include("user.urls")),
path('course/', include("course.urls")),
]