跳转到主要内容

为Django提供更简单的pdb调试

项目描述

简化Django的调试

每次想要在pdb中中断时,都要在源文件中添加pdb.set_trace()是很烦人的。

不要这样做。

这样做。

安装

使用pip安装

pip install django-pdb

将其添加到settings.py中。

对于1.7之前的Django,需要在覆盖runserver或test命令的任何应用之后添加(包括south和django.contrib.staticfiles)。

对于1.7之后的Django,需要在它们之前添加。

# Order is important and depends on your Django version.
# With Django 1.7+ put it towards the beginning, otherwise towards the end.
INSTALLED_APPS = (
    ...
    'django_pdb',
    ...
)

# Make sure to add PdbMiddleware after all other middleware.
# PdbMiddleware only activates when settings.DEBUG is True.
MIDDLEWARE_CLASSES = (
    ...
    'django_pdb.middleware.PdbMiddleware',
)

用法

manage.py runserver

如果URL包含 GET参数,则在视图开始时进入pdb。

如果URL包含ipdb GET参数,则在视图开始时进入ipdb。

此行为仅在settings.DEBUG = True时启用

bash: testproject/manage.py runserver
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 /test?pdb
function "myview" in testapp/views.py:7
args: ()
kwargs: {}

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

manage.py runserver --pdbmanage.py runserver --ipdb

在视图开始时进入pdb/ipdb

bash: testproject/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 /test
function "myview" in testapp/views.py:7
args: ()
kwargs: {}

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

manage.py test --pdb manage.py test --ipdb

在测试错误/失败时进入 pdb/ipdb

bash: testproject/manage.py test testapp --pdb
Creating test database for alias 'default'...
E
======================================================================
>>> test_error (testapp.tests.SimpleTest)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/tom/github/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)

事后分析模式

manage.py runserver --pm

事后分析模式,如果在视图中抛出异常,则进入 (i)pdb。这仅在没有其他应用程序覆盖 runserver 命令时有效。

POST_MORTEM = True

您还可以将 `POST_MORTEM = True` 添加到您的 `settings.py` 中,即使其他应用程序覆盖 `runserver`,也可以启用此选项。

过滤器

您还可以使用模板过滤器 pdbipdb 以这种方式在 (i)pdb 中探索模板变量

{% load pdb %}

{{ variable|pdb }}
{{ variable|ipdb }}
{{ variable|ipdb|a_filter_to_debug }}

示例

bash: testproject/manage.py runserver
Validating models...

0 errors found
Django version 1.4, using settings 'testproject.settings'
Development server is running at http://127.0.0.1:8000/
Quit the server with CONTROL-C.
> /Users/tom/github/django-pdb/django_pdb/templatetags/pdb_filters.py(14)pdb()
-> return element
(Pdb) element
"I'm the variable"
(Pdb) element = "another value"
(Pdb) c
[11/May/2012 11:22:53] "GET /filter/ HTTP/1.1" 200 37

这对于检查复杂对象是否未按预期行为或调试过滤器非常有用。

覆盖 test/runserver 的其他应用程序

manage.py test --pdb 如果您还有其他覆盖 test 命令的应用程序,只要它们使用 Python 的 unittest 框架即可工作。

请确保将 django_pdb 放在 INSTALLED_APPS 中任何冲突应用程序 之后,以便它们具有优先级。

项目详情


下载文件

下载您平台上的文件。如果您不确定要选择哪个,请了解有关 安装软件包 的更多信息。

源代码分发

django-pdb-0.6.2.tar.gz (8.3 kB 查看哈希值)

源代码

由以下机构支持