Flask系列(四)Flask实现简单页面登陆

from flask import Flask,render_template,request,redirect,session
app = Flask(__name__,template_folder='templates')
app.secret_key = "sdsfdsgdfgdfgfh"

@app.before_request
def process_request():
    if request.path=="/login":
        return None
    if not session.get("user_info"):
        return redirect("/login")
    return None
@app.after_request
def process_response(response):
    print(2222)
    return response

@app.route("/login",methods=["GET","POST"])
def login():
    if request.method=="GET":
        return render_template("login.html")
    else:
        # print(request.values)   #这个里面什么都有,相当于body
        username = request.form.get("username")
        password = request.form.get("password")
        if username=="shuyang" and password=="123":
            session["user_info"] = username
            # session.pop("user_info")  #删除session
            return redirect("/index")
        else:
            # return render_template("login.html",**{"msg":"用户名或密码错误"})
            return render_template("login.html",msg="用户名或者密码错误")


@app.route("/index",methods=["GET","POST"])
def index():
    # if not session.get("user_info"):
    #     return redirect("/login")
    return render_template("index.html")


if __name__ == '__main__':
    app.run(debug=True)

 

posted @ 2018-04-12 15:47  shuyang  阅读(705)  评论(0编辑  收藏  举报