Python - Django - 装饰器版的登陆校验

urls.py:

from django.conf.urls import url
from app01 import views


urlpatterns = [
    url(r'^login/', views.login),
    url(r'^home/', views.home),
    url(r'^index/', views.index),
    url(r'^logout/', views.logout),
]

views.py:

from django.shortcuts import render, redirect
from app01 import models

from functools import wraps


# 登录校验的装饰器
def check_login(func):
    @wraps(func)  # 装饰器修复技术
    def inner(request, *args, **kwargs):
        ret = request.get_signed_cookie("login", default="0", salt="whoami")
        if ret == "success":
            # 已经登录过,继续执行
            return func(request, *args, **kwargs)
        else:
            # 否则跳转到登录页面
            next_url = request.path_info  # 获取当前访问的 URL
            # next_url = request.get_full_path()  # 获取当前请求的路径和参数
            return redirect("/login/?next={}".format(next_url))
    return inner


def login(request):
    if request.method == "POST":
        username = request.POST.get("user")
        password = request.POST.get("pwd")
        next_url = request.GET.get("next")

        if username == "admin" and password == "admin":
            if next_url:
                rep = redirect(next_url)  # 得到一个响应对象
            else:
                rep = redirect("/home/")  # 得到一个响应对象
            # rep.set_cookie("login", "success")  # 设置 cookie
            rep.set_signed_cookie("login", "success", salt="whoami")  # 设置 cookie 并加盐
            return rep

    ret = request.get_signed_cookie("login", default="0", salt="whoami")
    if ret == "success":
        return redirect("/home/")  # 如果已经登录过,再访问 login,直接跳转到 home
    else:
        return render(request, "login.html")


def home(request):
    # ret = request.COOKIES.get("login")  # 获取 cookie 的 value
    ret = request.get_signed_cookie("login", default="0", salt="whoami")  # 获取加盐后 cookie 的 value
    if ret == "success":
        # cookie 验证成功
        return render(request, "home.html")
    else:
        return redirect("/login/")


@check_login
def index(request):
    return render(request, "index.html")


# 注销函数
def logout(request):
    rep = redirect("/login/")
    rep.delete_cookie("login")  # 删除 cookie
    return rep

login.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>登录页面</title>
</head>
<body>

<p>登录页面</p>

<form action="{{ request.get_full_path }}" method="post">
    {% csrf_token %}
    <p>
        账号:
        <input type="text" name="user">
    </p>
    <p>
        密码:
        <input type="text" name="pwd">
    </p>
    <p>
        <input type="submit" value="登录">
    </p>
</form>

</body>
</html>

home.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>个人信息页面</title>
</head>
<body>

<p>个人信息页面</p>

<a href="/logout/">注销</a>

</body>
</html>

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>主页面</title>
</head>
<body>

<p>主页面</p>

</body>
</html>

访问,http://127.0.0.1:8888/index/

 

输入,admin、admin

 

直接跳转到了 index 页面
这时再访问 login 页面,就会跳转到 home 页面

 

点击 “注销”

 

 回到了登录界面

posted @ 2019-08-31 22:18  Sch01aR#  阅读(840)  评论(0编辑  收藏  举报