试图刷新消息引发异常 [英] Trying to flash a message raises an exception

查看:137
本文介绍了试图刷新消息引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用 flash 来显示讯息,但这样做会引发异常。下面的代码演示了这个错误,并且如果调用 flash 被删除,那么这个工作正常。如何解决这个错误?

I want to use flash to show a message, but doing so raises an exception. The code below demonstrates the error, and works fine if the call to flash is removed. How do I fix this error?

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.


推荐答案

键,所以会话不可用。会话依靠一个秘密密钥来签署cookie防止篡改。消息闪烁依赖于会话。

As the error says, you have not set a secret key, so the session is unavailable. The session relies on a secret key to sign the cookie prevent tampering. Message flashing relies on the session.

设置 SECRET_KEY 配置项来解决这个错误。 b
$ b

Set the SECRET_KEY config item to fix this error.

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

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

这篇关于试图刷新消息引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆