Flask--session

session

和Django中设置session不同,Flask中的session被单独提出来了,不在request中;并且使用前需要设置secret_key参数

from flask import Flask,render_template,request,redirect,session

app = Flask(__name__)
app.secret_key = "xxxxx"		# 值可以随便写

@app.route("/login",methods=["POST","GET"])
def login():
    if request.method == "GET":
        return render_template("login.html")
    user = request.form.get("user")
    pwd = request.form.get("pwd")
    if user == "xxx" and pwd == "123":
        session["name"] = user						# 设置session
        return redirect("/index/")
    error = "用户名或密码错误"
    return render_template("login.html",error=error)

Flask的session存储在浏览器端,再次请求时会携带session,服务端进行解密校验

posted @ 2019-11-21 19:41  忘川的彼岸  阅读(128)  评论(0编辑  收藏  举报