flask中的登录验证 装饰器版 befor_request版

Posted chvv

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了flask中的登录验证 装饰器版 befor_request版相关的知识,希望对你有一定的参考价值。

一,第一种登录验证

  

@app01.route(/login,methods=["GET","POST"])
def login():
    if request.method == "GET":
        return render_template(login.html)

    username = request.form.get("username")
    password = request.form.get("password")
    if username == "chen" and password == "123":
        session[user] = username

        return redirect(/index)
    return render_template(login.html,error="用户名或密码错误")
    # return render_template(‘login.html‘,**{"error":"用户名或密码错误"})

@app01.route(/index)
def index():
    user = session.get("user")
    print(user)
    if not user:
        return redirect(/login)

    return render_template(index.html)

二 第二种登录验证---装饰器版

  

import functools
def check_login(func):
    @functools.wraps(func)
    def inner(*args,**kwargs):
        if session.get(user):
            return func(*args,**kwargs)
        else:
            return redirect(/login)

    return inner

@app.route(/login,methods=["GET","POST"])
def login():
    if request.method == "GET":
        return render_template("login.html")
    username = request.form.get("username")
    password = request.form.get("password")
    if username == "chen" and password == "123":
        session["user"] = username
        return redirect(/index)

    return render_template("login.html")

@app.route(/index)
@check_login
def index():
    user = session.get("user")
    if not user:
        return redirect(/login)
    return render_template("index.html",sut_dict = STUDENT_DICT)

@app.route(/detail/<int:nid>)
@check_login
def detail(nid):
    info = STUDENT_DICT[nid]

    return render_template("detail.html",info=info)


@app.route(/delete/<int:nid>)
@check_login
def delete(nid):
    del STUDENT_DICT[nid]
    return redirect(url_for(index))

三  第三种登录验证 before_request

  

@app.before_request
def check_login():
    if request.path == /login:
        return None
    if session.get(user):
        return None
    return redirect(/login)


@app.route(/login,methods=["GET","POST"])
def login():
    if request.method == "GET":
        return render_template("login.html")
    username = request.form.get("username")
    password = request.form.get("password")
    if username == "chen" and password == "123":
        session["user"] = username
        return redirect(/index)

    return render_template("login.html")

@app.route(/index)
def index():
    user = session.get("user")
    if not user:
        return redirect(/login)
    return render_template("index.html",sut_dict = STUDENT_DICT)


@app.route(/detail/<int:nid>)
def detail(nid):
    info = STUDENT_DICT[nid]

    return render_template("detail.html",info=info)


@app.route(/delete/<int:nid>)
def delete(nid):
    del STUDENT_DICT[nid]
    return redirect(url_for(index))

 

以上是关于flask中的登录验证 装饰器版 befor_request版的主要内容,如果未能解决你的问题,请参考以下文章

Flask--登录验证(多个装饰器)

Python Flask装饰器登录验证

Flask10 登录模块表单框架表单渲染表单验证bookie请求之前钩子g对象编写装饰器

如何在 Flask 中实现登录所需的装饰器

flask_login 中的 login_required 装饰器未重定向到上一页

Flask初识,第十篇,Flask中的装饰器 before_request after_request