为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包含
如果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 --pdb 或 manage.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`,也可以启用此选项。
过滤器
您还可以使用模板过滤器 pdb 或 ipdb 以这种方式在 (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 的哈希值
算法 | 哈希摘要 | |
---|---|---|
SHA256 | eb8f4ed1a7f2b9c67944414688fd39a6f38f410dd7461d417512d72359f0472d |
|
MD5 | 0fb952f0ab99d4ff67a0c6c6e6547aca |
|
BLAKE2b-256 | 97e825c9fa3a6c27da51d0b6dcd8308412a37a9f5bc7e9f79bb0f10b1dffc0f5 |