python
config_error
ai_generated
true
RuntimeError: 会话不可用,因为未设置密钥。请为应用设置唯一且保密的 secret_key。
RuntimeError: The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.
ID: python/flask-secret-key-required
80%修复率
90%置信度
0证据数
2024-01-19首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| 3.x | active | — | — | — |
根因分析
Flask 会话(或 CSRF)需要 app.secret_key,但从未配置,常见于测试或新部署环境。
English
Flask session (or CSRF) requires app.secret_key but it was never configured, often in tests or a fresh deploy.
解决方案
-
95% 成功率
Set a secret key from environment: app.config['SECRET_KEY'] = os.environ['SECRET_KEY']
-
85% 成功率
For tests only, generate a fixed key: app.config['SECRET_KEY'] = 'test-key'
无效尝试
常见但无效的做法:
-
90% 失败
Permanence affects cookie lifetime, not the required signing key.
-
70% 失败
Most session backends still need a secret key for signing.