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)

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

其他回答

调试python的最简单的方法——尤其是对习惯于Visual Studio的程序员来说——是使用PTVS (python Tools for Visual Studio)。 步骤很简单:

从https://microsoft.github.io/PTVS/下载并安装它 设置断点并按F5。 您的断点被击中,您可以查看/更改变量,就像调试c# / c++程序一样简单。 这就是全部:)

如果你想使用PTVS调试Django,你需要做以下事情:

在Project settings - General选项卡中,将“Startup File”设置为“manage.py”,这是Django程序的入口点。 在“项目设置-调试”选项卡中,将“脚本参数”设置为“runserver—noreload”。这里的关键点是“—noreload”。如果你不设置它,你的断点就不会被击中。 享受它。

我使用pyDev与Eclipse真的很好,设置断点,进入代码,查看任何对象和变量的值,尝试它。

我非常喜欢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上,当然欢迎贡献。

从我自己的经验来看,有两种方法:

使用ipdb,这是一个像pdb一样的增强调试器。 导入ipdb;ipdb.set_trace()或breakpoint() (from python3.7) 使用django shell,只需使用下面的命令。这在您开发新视图时非常有用。 Python manage.py shell