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


当前回答

有很多方法可以做到这一点,但最直接的是简单 使用Python调试器。只需在Django视图函数中添加如下一行:

import pdb; pdb.set_trace()

or

breakpoint()  #from Python3.7

如果您试图在浏览器中加载该页,浏览器将挂起,并提示您对实际执行的代码进行调试。

但是也有其他的选择(我不推荐):

* return HttpResponse({variable to inspect})

* print {variable to inspect}

* raise Exception({variable to inspect})

但是对于所有类型的Python代码,强烈推荐使用Python调试器(pdb)。如果您已经开始使用pdb,那么您还需要了解使用ipython进行调试的IPDB。

对pdb有一些更有用的扩展

pdb++,由Antash建议。

pudb,由PatDuJour建议。

在Django中使用Python调试器,由Seafangs建议。

其他回答

添加导入pdb;pdb.set_trace()或breakpoint()(形式python3.7)在Python代码中的相应行中执行。执行将在交互式shell中停止。在shell中,您可以执行Python代码(即打印变量)或使用如下命令:

C继续执行 N步到同一函数中的下一行 S步到这个函数或被调用函数的下一行 Q退出调试器/执行

参见:https://poweruser.blog/setting-a-breakpoint-in-python-438e23fe6b28

我已经把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上,当然欢迎贡献。

有很多方法可以做到这一点,但最直接的是简单 使用Python调试器。只需在Django视图函数中添加如下一行:

import pdb; pdb.set_trace()

or

breakpoint()  #from Python3.7

如果您试图在浏览器中加载该页,浏览器将挂起,并提示您对实际执行的代码进行调试。

但是也有其他的选择(我不推荐):

* return HttpResponse({variable to inspect})

* print {variable to inspect}

* raise Exception({variable to inspect})

但是对于所有类型的Python代码,强烈推荐使用Python调试器(pdb)。如果您已经开始使用pdb,那么您还需要了解使用ipython进行调试的IPDB。

对pdb有一些更有用的扩展

pdb++,由Antash建议。

pudb,由PatDuJour建议。

在Django中使用Python调试器,由Seafangs建议。

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

pdb的

import pdb
pdb.set_trace()

对于ipdb

import ipdb
ipdb.set_trace()

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

我使用PyCharm(与eclipse相同的pydev引擎)。真的帮助我能够直观地逐级检查我的代码,并看到发生了什么。