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代码?


当前回答

到目前为止,几乎所有的东西都已经提到了,所以我只补充一点,可以使用ipdb.set_trace()而不是pdb.set_trace(),它使用iPython,因此更强大(自动完成和其他好东西)。这需要ipdb包,所以你只需要pip安装ipdb

其他回答

我使用PyCharm并一直支持它。我花了一点钱,但我不得不说我从中得到的好处是无价的。我尝试过从控制台调试,我确实很信任那些能做到这一点的人,但对我来说,能够直观地调试我的应用程序是很棒的。

我不得不说,PyCharm确实占用了很多内存。但话说回来,生活中没有什么好东西是免费的。他们刚刚发布了最新版本3。它也可以很好地与Django, Flask和谷歌AppEngine一起使用。所以,总而言之,我认为这是任何开发人员都可以拥有的一个非常方便的工具。

如果你还没有使用它,我建议你试用30天,看看PyCharm的强大功能。我相信还有其他可用的工具,比如Aptana。但我想我也喜欢PyCharm的外观。我觉得在那里调试我的应用程序很舒服。

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

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

我已经把django-pdb推到了PyPI。 这是一个简单的应用程序,这意味着你不需要编辑你的源代码,每次你想进入pdb。

安装只是…

PIP安装django-pdb 添加'django_pdb'到你的INSTALLED_APPS

你现在可以运行:manage.py runserver——pdb在每个视图的开始进入pdb…

bash: manage.py runserver --pdb
Validating models...

0 errors found
Django version 1.3, using settings 'testproject.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.

GET /
function "myview" in testapp/views.py:6
args: ()
kwargs: {}

> /Users/tom/github/django-pdb/testproject/testapp/views.py(7)myview()
-> a = 1
(Pdb)

然后运行:manage.py test——pdb在测试失败/错误时进入pdb…

bash: manage.py test testapp --pdb
Creating test database for alias 'default'...
E
======================================================================
>>> test_error (testapp.tests.SimpleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File ".../django-pdb/testproject/testapp/tests.py", line 16, in test_error
    one_plus_one = four
NameError: global name 'four' is not defined
======================================================================

> /Users/tom/github/django-pdb/testproject/testapp/tests.py(16)test_error()
-> one_plus_one = four
(Pdb)

该项目托管在GitHub上,当然欢迎贡献。

从我的角度来看,我们可以将常见的代码调试任务分解为三种不同的使用模式:

Something has raised an exception: runserver_plus' Werkzeug debugger to the rescue. The ability to run custom code at all the trace levels is a killer. And if you're completely stuck, you can create a Gist to share with just a click. Page is rendered, but the result is wrong: again, Werkzeug rocks. To make a breakpoint in code, just type assert False in the place you want to stop at. Code works wrong, but the quick look doesn't help. Most probably, an algorithmic problem. Sigh. Then I usually fire up a console debugger PuDB: import pudb; pudb.set_trace(). The main advantage over [i]pdb is that PuDB (while looking as you're in 80's) makes setting custom watch expressions a breeze. And debugging a bunch of nested loops is much simpler with a GUI.

啊,是的,模板的问题。最常见的问题(对我和我的同事来说)是错误的上下文:要么你没有变量,要么你的变量没有某些属性。如果你正在使用调试工具栏,只检查“模板”部分的上下文,或者,如果这还不够,在你的上下文填充后,在视图的代码中设置一个断点。

就这样。

如果在django开发中使用Aptana,请注意:http://www.youtube.com/watch?v=qQh-UQFltJQ

如果没有,考虑使用它。