我想使用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.
python大神给出的解决方案
如错误所示,您尚未设置密钥,因此会话不可用。会话依靠密钥对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'