简体   繁体   English

你如何在Flask中调试url路由?

[英]How do you debug url routing in Flask?

I'm developing a Flask website using Visual Studio 2013's PythonTools, which has its own debugger, and that allows me to step through the initial setup code, up until app.run() 我正在使用Visual Studio 2013的PythonTools开发一个Flask网站,它有自己的调试器,这使我可以逐步完成初始设置代码,直到app.run()

However the code I want to debug is the routing code, like this: 但是我要调试的代码是路由代码,如下所示:

@app.route('/')
def url_index():
     return render_template('index.html') 

I know that function is running because the server does respond with index.html, but if I put a breakpoint on the last line, it'll never get hit. 我知道函数正在运行,因为服务器确实以index.html响应,但是如果我在最后一行放置一个断点,它将永远不会被击中。

Is there any way to debug these routing functions? 有没有办法调试这些路由功能? Flask says it comes with a debugger but how do I use it? Flask表示它附带一个调试器,但我该如何使用它? Will it be compatible with Visual Studio? 它是否与Visual Studio兼容?

6 months later, and while it still doesn't look possible to automatically debug URL routing in flask, you can manually attach a debugger to the flask process, though you'll have to re-add it if you restart the server or if the auto-reloader detects changes in your .py files and restarts. 6个月之后,虽然它仍然无法自动调试烧瓶中的URL路由,但您可以手动将调试器附加到烧瓶过程,但如果重新启动服务器或者如果需要重新添加它,则必须重新添加它。自动重新加载器检测.py文件中的更改并重新启动。

Just go: Tools -> Attach to Process and select the Python.exe that is not greyed out (that's the initial flask code that visual studio is already debugging), and then do something that would cause the breakpoint to be hit (eg reload the page), and you should have success. 只需转到: Tools -> Attach to Process并选择不是灰色的Python.exe(这是visual studio已经调试的初始烧录代码),然后做一些会导致断点被击中的东西(例如重新加载页面),你应该有成功。

For the Flask debugger, you can set app.debug to True : 对于Flask调试器,您可以将app.debug设置为True

app.debug = True

or 要么

app.run(debug=True)

And then: 然后:

@app.route('/')
def index():
   raise
   return render_template('index.html') 

And then you can debug the function with the Flask debugger in your browser. 然后,您可以在浏览器中使用Flask调试器调试该函数。

Sadly the current version of PTVS doesn't support Flask projects. 可悲的是,当前版本的PTVS不支持Flask项目。

Good thing is: the already released PTVS 2.1 alpha does: http://pytools.codeplex.com/wikipage?title=Flask 好消息是:已经发布的PTVS 2.1 alpha确实: http ://pytools.codeplex.com/wikipage?title = Flask

You can turn off reloading with debug mode by using 您可以使用调试模式关闭重新加载

app.run(debug=True, use_reloader=False)

The Flask error handling docs go into the details of the debugging options. Flask错误处理文档详细介绍了调试选项。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM