So, I started learning to code in Python and later Django. The first times it was hard looking at tracebacks and actually figure out what I did wrong and where the syntax error was. Some time has passed now and some way along the way, I guess I got a routine in debugging my Django code. As this was done early in my coding experience, I sat down and wondered if how I was doing this was ineffective and could be done faster. I usually manage to find and correct the bugs in my code, but I wonder if I should be doing it faster?

我通常只使用Django启用时提供的调试信息。当事情确实像我想象的那样结束时,我用一个语法错误破坏了代码流,并查看流中那个点的变量,以找出代码在哪里做了与我想要的不同的事情。

但这种情况还能改善吗?是否有一些更好的工具或方法来调试Django代码?


当前回答

调试Django代码的最佳选择之一是通过wdb: https://github.com/Kozea/wdb

wdb works with python 2 (2.6, 2.7), python 3 (3.2, 3.3, 3.4, 3.5) and pypy. Even better, it is possible to debug a python 2 program with a wdb server running on python 3 and vice-versa or debug a program running on a computer with a debugging server running on another computer inside a web page on a third computer! Even betterer, it is now possible to pause a currently running python process/thread using code injection from the web interface. (This requires gdb and ptrace enabled) In other words it's a very enhanced version of pdb directly in your browser with nice features.

安装并运行服务器,并在代码中添加:

import wdb
wdb.set_trace()

作者认为,pdb的主要区别是:

For those who don’t know the project, wdb is a python debugger like pdb, but with a slick web front-end and a lot of additional features, such as: Source syntax highlighting Visual breakpoints Interactive code completion using jedi Persistent breakpoints Deep objects inspection using mouse Multithreading / Multiprocessing support Remote debugging Watch expressions In debugger code edition Popular web servers integration to break on error In exception breaking during trace (not post-mortem) in contrary to the werkzeug debugger for instance Breaking in currently running programs through code injection (on supported systems)

它有一个很棒的基于浏览器的用户界面。一种使用的乐趣!:)

其他回答

我强烈建议使用PDB。

import pdb
pdb.set_trace()

您可以检查所有变量值,进入函数和更多。 https://docs.python.org/2/library/pdb.html

用于检查对数据库的各种请求、响应和命中。我正在使用django-debug-toolbar https://github.com/django-debug-toolbar/django-debug-toolbar

我非常喜欢Werkzeug的交互式调试器。它类似于Django的调试页面,除了在回溯的每一层都有一个交互式shell。如果你使用django-extensions,你会得到一个runserver_plus管理命令,它会启动开发服务器,并在异常时为你提供Werkzeug的调试器。

当然,您应该只在本地运行它,因为它赋予任何使用浏览器的人在服务器上下文中执行任意python代码的权利。

对于那些不小心将pdb添加到实时提交的人,我可以建议这个扩展#Koobz的答案:

@register.filter 
def pdb(element):
    from django.conf import settings
    if settings.DEBUG:    
        import pdb
        pdb.set_trace()
    return element

正如在其他文章中提到的,在你的代码中设置断点,然后遍历代码,看看它是否像你预期的那样运行,这是学习像Django这样的东西的好方法,直到你对它的所有行为以及你的代码在做什么有很好的感觉。

要做到这一点,我建议使用WingIde。就像其他提到的ide一样,很好很容易使用,布局很好,也很容易设置断点,计算/修改堆栈等。完美的可视化你的代码正在做什么,因为你逐步通过它。我是它的超级粉丝。

我还使用PyCharm——它有出色的静态代码分析,有时可以帮助你在意识到问题之前发现问题。

如前所述,django-debug-toolbar是必不可少的- https://github.com/django-debug-toolbar/django-debug-toolbar

虽然不是一个明确的调试或分析工具,但我最喜欢的一个是SQL打印中间件,可以从Django Snippets (https://djangosnippets.org/snippets/290/)上获得

这将显示视图生成的SQL查询。这将使您很好地了解ORM正在做什么,以及您的查询是否有效,或者您是否需要重做代码(或添加缓存)。

我发现它对于在开发和调试应用程序时监视查询性能非常有用。

还有一个技巧——我为自己的使用对它进行了稍微修改,只显示摘要而不显示SQL语句....所以我总是在开发和测试时使用它。我还补充说,如果len(connection.queries)大于预定义的阈值,它会显示一个额外的警告。

然后,如果我发现发生了一些不好的事情(从性能或查询数量的角度来看),我会返回SQL语句的完整显示,以查看到底发生了什么。当你与多个开发人员一起开发一个大型Django项目时,这非常方便。

使用PDB或ipdb。这两者的区别是ipdb支持自动完成。

pdb的

import pdb
pdb.set_trace()

对于ipdb

import ipdb
ipdb.set_trace()

执行换行按n键,继续按c键。 使用帮助(pdb)检查更多选项