Django基础之Session版登录验证
Posted yang-wei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Django基础之Session版登录验证相关的知识,希望对你有一定的参考价值。
from functools import wraps def check_login(func): @wraps(func) def inner(request, *args, **kwargs): next_url = request.get_full_path() if request.session.get("user"): return func(request, *args, **Kwargs) else: return redirect("/login/?next={}".format(next_url)) return inner def login(request): if request.method == "POST": username = request.POST.get("username") password = request.POST.get("password") if username == "yang" and password = "123": # 设置session request.session["user"] = user #获取跳到登录页面之前的URL next_url = request.GET.get("next") # 如果有,就跳转到登录之前的URL if next_url: return redirect(next_url) # 否则默认跳转到index页面 else: return redirect("/index/") return render(request, "login.html") @check_login def logout(request): # 删除所有当前请求相关的session request.session.delete() return redirect("/login/") @check_login def index(request): current_user = request.session.get("user", None) return render(request, "index.html", {"user": currrent_user})
以上是关于Django基础之Session版登录验证的主要内容,如果未能解决你的问题,请参考以下文章