尝试刷写消息引发异常

3

我想使用flash来显示一条消息,但这样做会引发异常。下面的代码演示了错误,并且如果删除对flash的调用,它可以正常工作。我该如何修复这个错误?

from flask import Flask, flash

app = Flask(__name__)

@app.route('/')
def index():
    flash('Entered')
    return 'Completed'

app.run(debug=True)

RuntimeError: the session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.
2个回答

5

根据错误提示,您没有设置密钥,因此会话不可用。会话依赖于密钥对cookie进行签名以防止篡改。消息闪现也依赖于会话。

设置SECRET_KEY配置项以解决此错误。

# set as part of the config
SECRET_KEY = 'many random bytes'

# or set directly on the app
app.secret_key = 'many random bytes'

0
在你的 run.py 文件中。
from tc import app
app.secret_key='12345'
app.run(debug=True)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接