Python2的Flask坑(一)--Set the secret_key on the application to something unique and secret

本文介绍了在Python Flask应用中设置secret_key的必要性,强调了该设置对于使用session和flash功能的安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

增加这行代码,值可以随便写

app.secret_key = "sdkfjlqjluio23u429037907!@#!@#!@@" 

使用session、flash需要secret_key

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

Traceback (most recent call last)
File "/home/python/.virtualenvs/Flask_py/lib/python2.7/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/home/python/.virtualenvs/Flask_py/lib/python2.7/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/python/.virtualenvs/Flask_py/lib/python2.7/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/python/.virtualenvs/Flask_py/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/home/python/.virtualenvs/Flask_py/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/python/.virtualenvs/Flask_py/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/python/.virtualenvs/Flask_py/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/home/python/.virtualenvs/Flask_py/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/python/Desktop/Flask/FLask_test3/test5_tempvar.py", line 11, in index
flash(u"闪现消息1")
File "/home/python/.virtualenvs/Flask_py/lib/python2.7/site-packages/flask/helpers.py", line 366, in flash
session['_flashes'] = flashes
File "/home/python/.virtualenvs/Flask_py/lib/python2.7/site-packages/werkzeug/local.py", line 350, in __setitem__
self._get_current_object()[key] = value
File "/home/python/.virtualenvs/Flask_py/lib/python2.7/site-packages/flask/sessions.py", line 126, in _fail
raise RuntimeError('the session is unavailable because no secret '
RuntimeError: the session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret.
D:\PythonProject\.venv\Scripts\python.exe D:\PythonProject\app.py * Serving Flask app 'app' * Debug mode: on WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on http://127.0.0.1:5000 Press CTRL+C to quit * Restarting with stat * Debugger is active! * Debugger PIN: 120-624-777 This runs only once on the first request! 127.0.0.1 - - [10/Jun/2025 16:50:28] "GET / HTTP/1.1" 200 - 127.0.0.1 - - [10/Jun/2025 16:50:28] "GET /static/css/style.css HTTP/1.1" 404 - 127.0.0.1 - - [10/Jun/2025 16:50:28] "GET /static/js/main.js HTTP/1.1" 404 - 127.0.0.1 - - [10/Jun/2025 16:50:30] "GET /doctor_list?department=1&date=2025-06-10 HTTP/1.1" 500 - Traceback (most recent call last): File "D:\PythonProject\.venv\Lib\site-packages\flask\app.py", line 1536, in __call__ return self.wsgi_app(environ, start_response) ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\PythonProject\.venv\Lib\site-packages\flask\app.py", line 1514, in wsgi_app response = self.handle_exception(e) File "D:\PythonProject\.venv\Lib\site-packages\flask\app.py", line 1511, in wsgi_app response = self.full_dispatch_request() File "D:\PythonProject\.venv\Lib\site-packages\flask\app.py", line 919, in full_dispatch_request rv = self.handle_user_exception(e) File "D:\PythonProject\.venv\Lib\site-packages\flask\app.py", line 917, in full_dispatch_request rv = self.dispatch_request() File "D:\PythonProject\.venv\Lib\site-packages\flask\app.py", line 902, in dispatch_request return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^ File "D:\PythonProject\app.py", line 64, in doctor_list flash(f'ID为 {department_id} 的科室不存在', 'warning') ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "D:\PythonProject\.venv\Lib\site-packages\flask\helpers.py", line 335, in flash session["_flashes"] = flashes ~~~~~~~^^^^^^^^^^^^ File "D:\PythonProject\.venv\Lib\site-packages\flask\sessions.py", line 104, in _fail raise RuntimeError( ^ RuntimeError: The session is unavailable because no secret key was set. Set the secret_key on the application to something unique and secret. 127.0.0.1 - - [10/Jun/2025 16:50:30] "GET /doctor_list?__debugger__=yes&cmd=resource&f=style.css HTTP/1.1" 304 - 127.0.0.1 - - [10/Jun/2025 16:50:30] "GET /doctor_list?__debugger__=yes&cmd=resource&f=debugger.js HTTP/1.1" 304 - 127.0.0.1 - - [10/Jun/2025 16:50:30] "GET /doctor_list?__debugger__=yes&cmd=resource&f=console.png&s=v8WikeQeBL2PiowtaTJt HTTP/1.1" 200 -
最新发布
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值