【问题标题】:Flask and Heroku: Key Error while getting session variableFlask 和 Heroku:获取会话变量时出现键错误
【发布时间】:2020-06-06 17:08:56
【问题描述】:

所以我试图在 Heroku 中建立一个在本地完美运行的基本站点。我在用户登录时有以下代码可以正常执行:

@app.route("/callback")
def callback():
    token = oauth.authorize_access_token()
    session["userinfo"] = oauth.parse_id_token(token)
    name = session['userinfo']['name']
    email = session['userinfo']['email']

但是当我尝试在不同的 def 中使用会话变量时,我在此处收到日志错误 'KeyError: 'userinfo'':

@app.route("/profile", methods=["GET", "POST"])
def profile():
    if request.data:
        results = request.json
        con = db_connect()
        c = con.cursor()
        c.execute("select hash from users where email = %s", (session['userinfo']['email'],))

我已经在控制台的回调中打印了名称以验证 Auth0 部分是否正常工作,但我猜测 Heroku 出于某种原因没有很好地处理会话变量?

【问题讨论】:

  • 你解决了吗?

标签: python html flask heroku


【解决方案1】:

如果您在 Heroku 中使用配置变量,请检查以确保它们没有任何连字符。 Heroku 的文档中提到了这一点。我有一个应用程序在本地运行得非常好,但在 Heroku 上运行时也出现了 KeyError。原来我的配置变量键有连字符,将它们更改为下划线后,错误消失了!

【讨论】:

    猜你喜欢
    • 2018-11-17
    • 1970-01-01
    • 1970-01-01
    • 2012-03-21
    • 1970-01-01
    • 2018-10-14
    • 2017-12-17
    • 2013-08-10
    • 1970-01-01
    相关资源
    最近更新 更多