开启了app.debug=True/app.run(debug=True) Flask仍然无法开启调试模式【Pycharm环境】

博主在写Python Flask的Waf后台管理界面时,想在Pycharm中启用调试模式,发现按网上流行方式无法开启。研究发现Flask 1.0后不支持用debug =True开启。解决办法是在Pycharm 2018环境通过Edit Configurations勾选Flask_Debug,Linux环境改变Flask_Debug值,1开0关。

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

最近在写python flask 的Waf后台管理界面,想要启用调试模式,发现安装目前网上流行的两种方式均无法在我的Pycharm中打开调试模式。
1)直接在对象上设置

app.debug=True
app.run()

2)作为run的参数传入进去

app.run(debug=True)

简单研究了一下后,发现是flask 更新到1.0以后 不支持使用debug =True 来开启调试模式了。
Prior to Flask 1.0 the FLASK_ENV environment variable was not supported and you needed to enable debug mode by exporting FLASK_DEBUG=1. This can still be used to control debug mode, but you should prefer setting the development environment as shown above.

解决办法:
Pycharm 2018环境中可以直接右键运行按钮左边下拉菜单的Edit Configurations,勾上Flask_Debug后面的小勾就好了。
linux环境的话直接
export Flask_Debug = 1

就可以了。

总之,只要改变了Flask_Debug的值就可以开启调试模式了,1为开,0 为关

``` from flask import Flask,render_template,request,redirect,url_for,session import unittest app=Flask(__name__) #主页 @app.route('/') def index(): if "username" in session: return redirect(url_for("success",name = session["username"])) return redirect(url_for("login")) #登录成功页面 @app.route('/success/<name>') def success(name): return f"{name} login success!" @app.route('/login',methods = ["GET","POST"]) def login(): if request.method == "POST": session["username"] = request.form.get("username") return redirect(url_for("success",name = session["username"])) return render_template('login1.html') if __name__ == '__main__': app.run(debug=True)```E:\202312100217-Ljw\flask_web\envs\python.exe "D:/Program Files/JetBrains/PyCharm 2023.1/plugins/python/helpers/pycharm/_jb_pytest_runner.py" --target get_post.py::test Testing started at 8:29 ... D:/Program Files/JetBrains/PyCharm 2023.1/plugins/python/helpers/pycharm/_jb_pytest_runner.py:8: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html from pkg_resources import iter_entry_points Launching pytest with arguments get_post.py::test --no-header --no-summary -q in E:\202312100217-Ljw\路由 ============================= test session starts ============================= collecting ... collected 1 item get_post.py::test FAILED [100%] get_post.py:3 (test) @app.route('/',methods = ['GET','POST']) def test(): > if request.method == 'GET': get_post.py:6: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ..\flask_web\envs\lib\site-packages\werkzeug\local.py:432: in __get__ obj = instance._get_current_object() ..\flask_web\envs\lib\site-packages\werkzeug\local.py:554: in _get_current_object return self.__local() # type: ignore _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ name = 'request' def _lookup_req_object(name): top = _request_ctx_stack.top if top is None: > raise RuntimeError(_request_ctx_err_msg) E RuntimeError: Working outside of request context. E E This typically means that you attempted to use functionality that needed E an active HTTP request. Consult the documentation on testing for E information about how to avoid this problem. ..\flask_web\envs\lib\site-packages\flask\globals.py:33: RuntimeError ============================== 1 failed in 0.24s ============================== Process finished with exit code 1
03-20
python代码为: from flask import Flask,request,render_template app = Flask(__name__) @app.route('/',methods = ['GET','POST']) def test(): if request.method == 'GET': return render_template('login.html') else: return 'I get POST!' if __name__ == '__main__': app.run(debug=True) html文件代码为: <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Get与Post请求</title> </head> <body> <h2>如果收到Get请求,返回这个HTML页面</h2> <form method="post"> <button type="submit"> 按下我发送POST请求 </button> </form> </body> </html> 运行时出现以上错误,是什么原因,要如何解决: E:\202312100217-Ljw\flask_web\envs\python.exe "D:/Program Files/JetBrains/PyCharm 2023.1/plugins/python/helpers/pycharm/_jb_pytest_runner.py" --path E:\202312100217-Ljw\路由\get_post.py Testing started at 8:44 ... D:/Program Files/JetBrains/PyCharm 2023.1/plugins/python/helpers/pycharm/_jb_pytest_runner.py:8: DeprecationWarning: pkg_resources is deprecated as an API. See https://setuptools.pypa.io/en/latest/pkg_resources.html from pkg_resources import iter_entry_points Launching pytest with arguments E:\202312100217-Ljw\路由\get_post.py --no-header --no-summary -q in E:\202312100217-Ljw\路由 ============================= test session starts ============================= collecting ... collected 1 item get_post.py::test FAILED [100%] get_post.py:3 (test) @app.route('/',methods = ['GET','POST']) def test(): > if request.method == 'GET': get_post.py:6: _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ ..\flask_web\envs\lib\site-packages\werkzeug\local.py:432: in __get__ obj = instance._get_current_object() ..\flask_web\envs\lib\site-packages\werkzeug\local.py:554: in _get_current_object return self.__local() # type: ignore _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ name = 'request' def _lookup_req_object(name): top = _request
03-13
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值