Skip to content

Latest commit

 

History

History
116 lines (73 loc) · 2.31 KB

File metadata and controls

116 lines (73 loc) · 2.31 KB

django debug tools

Django Debug Toolbar是一个调试Django web应用程序的有用工具。工具栏帮助您了解应用程序的功能并识别问题。它通过提供提供有关当前请求和响应的调试信息的面板来实现这一点。

我们也可以用他来做部分性能分析,分析请求的性能,以及SQL的性能。

安装与配置

参考:https://django-debug-toolbar.readthedocs.io/en/latest/installation.html

1.通过pip安装:

pip install django-debug-toolbar

2.添加安装应用

settings.py文件中配置INSTALLED_APPS:

INSTALLED_APPS = [
    # ...
    "debug_toolbar",
    # ...
]

3.添加URLs

添加django-debug-toolbar URLs 到项目的URLconf,即:mysite/urls.py文件

# ...
from debug_toolbar.toolbar import debug_toolbar_urls

urlpatterns = [
    # ...
] + debug_toolbar_urls()

4.添加中间件

Debug Toolbar主要的中间件中实现,在settings.py文件中配置MIDDLEWARE:

MIDDLEWARE = [
    # ...
    "debug_toolbar.middleware.DebugToolbarMiddleware",
    # ...
]

5.配置内部IP

只有当你的IP地址在Django的INTERNAL_IPS设置中列出时,Debug toolbar 才会显示。对于本地开发,必须将127.0.0.1添加到INTERNAL_IPS。在settings.py文件中配置INTERNAL_IPS:

INTERNAL_IPS = [
    # ...
    "127.0.0.1",
    # ...
]

完成以上5步,就可以在项目中愉快的使用 Debug Toolbar 了。

使用

访问django应用任意页面,右侧显示DjDT按钮。

点击DjDT按钮显示 Debug toolbar 工具列表

🚨问题:【DjDT在】部分页面无法显示的问题。

view视图返回的页面一定要包含<body></body>标签,例如results.html页面:

<body> 
<h1>{{ question.question_text }}</h1>

<ul>
{% for choice in question.choice_set.all %}
    <li>{{ choice.choice_text }} -- {{ choice.votes }} vote{{ choice.votes|pluralize }}</li>
{% endfor %}
</ul>

<a href="{% url 'polls:detail' question.id %}">Vote again?</a>

</body>

分析

  • 查看页面的响应。

  • 查看历史URL请求。

  • 查看请求信息。

  • 查看SQL。