flask 使用session时报错:The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.

发布于 2020-01-04  574 次阅读


报错前源代码:

@baseViews.route("/login/",methods = ["GET","POST"])
def login():
    if request.method == "GET":
        return render_template("login/index.html")
    elif request.method == "POST":
        username = request.form.get("username")
        response = Response("登陆成功 %s" % username)
        #response.set_cookie("username", username)
        session['username'] = username
        return response
@views.route("/getCookie")
def get_cookie():
    # username = request.cookies.get("username")
    username = session.get("username")
    return "Welcome %s" % username

报错:

原文:
The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.
翻译:
会话不可用,因为没有设置密钥应用程序上的secret_key是唯一和秘密的。

修改代码:

在app初始化的时候加入以下代码

app.secret_key = "你自己设置的密钥"

像烟花也是过一生,像樱花也是一生,只要亮过和盛开不就好了么?