主要应用
1. 查看 django + rest_framework 的接口 调用的sql 并提供优化意见
2. 打开页面的时间 分析
Django Debug Toolbar
Django调试工具栏
可配置的面板,显示有关当前请求/响应的各种调试信息
django-debug-toolbar-request-history
Django调试工具栏的请求历史记录面板
将请求历史记录面板添加到Django Debug Toolbar中,以查看不同请求的统计信息(带有ajax支持选项)
我的环境
python 3.6
Django 2.2.5
djangorestframework 3.10.2
mysqlclient 1.4.4 (注意,有些sql分析不显示的 原因 是这个版本问题)
django-debug-toolbar 2.2
django-debug-toolbar-request-history 0.1.1
安装
pip install -i https://mirrors.aliyun.com/pypi/simple/ django-debug-toolbar
pip install -i https://mirrors.aliyun.com/pypi/simple/ django-debug-toolbar-request-history
django-debug-toolbar 配置
这部分也可以查看 [django-debug-toolbar 文档](https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#getting-the-code)
1.前提条件 在settings.py 页面
确保’django.contrib.staticfiles’被正确设置,并添加 ‘debug_toolbar’到您的INSTALLED_APPS设置:
INSTALLED_APPS = [
# ...
'django.contrib.staticfiles',
# ...
'debug_toolbar',
]
STATIC_URL = '/static/'
2.启用中间件 在settings.py 页面
的顺序MIDDLEWARE很重要。您应该尽早在列表中包括“调试工具栏”中间件。但是,它必须位于对响应内容进行编码的任何其他中间件之后,例如 GZipMiddleware。
MIDDLEWARE = [
# ...
'debug_toolbar.middleware.DebugToolbarMiddleware',
# ...
]
3.配置内部 在settings.py 页面
仅当INTERNAL_IPS设置中列出了您的IP地址时,才会显示“调试工具栏”
INTERNAL_IPS = [
# ...
'127.0.0.1',
'10.xxx.xx.xx',
# ...
]
4.配置 在settings.py 页面
# 这个对应DEBUG_TOOLBAR 显示的栏位 ,可以自己定义,修改
DEBUG_TOOLBAR_PANELS = [
'ddt_request_history.panels.request_history.RequestHistoryPanel', # 这是django-debug-toolbar-request-history
'debug_toolbar.panels.versions.VersionsPanel',
'debug_toolbar.panels.timer.TimerPanel',
'debug_toolbar.panels.settings.SettingsPanel',
'debug_toolbar.panels.headers.HeadersPanel',
'debug_toolbar.panels.request.RequestPanel',
'debug_toolbar.panels.sql.SQLPanel',
'debug_toolbar.panels.staticfiles.StaticFilesPanel',
'debug_toolbar.panels.templates.TemplatesPanel',
'debug_toolbar.panels.cache.CachePanel',
'debug_toolbar.panels.signals.SignalsPanel',
'debug_toolbar.panels.logging.LoggingPanel',
'debug_toolbar.panels.redirects.RedirectsPanel',
]
DEBUG_TOOLBAR_CONFIG = {
"JQUERY_URL": '//cdn.bootcss.com/jquery/2.2.4/jquery.min.js', # 使用国内的 JQUERY_URL
'RESULTS_STORE_SIZE': 100, # 已存储请求的数量
}
5.设置URL 在urls.py 页面
from django.conf import settings
from django.conf.urls import include, url # For django versions before 2.0
from django.urls import include, path # For django versions from 2.0 and up
if settings.DEBUG:
import debug_toolbar
urlpatterns = [
path('__debug__/', include(debug_toolbar.urls)),
# For django versions before 2.0:
# url(r'^__debug__/', include(debug_toolbar.urls)),
] + urlpatterns
使用
运行项目 查看 , 如果用vue 的前端,debug toolbar 是无法正确显示的,建议切换到 DRF 的docs 页面

image.png
运行其中一个 POST方法 (如果没有 Request History 请安装 django-debug-toolbar-request-history

image.png
单击 Request History 会显示 请求历史

image.png
单击 其中的一个方法 ,再 点击 SQL 就可以查看 ORM 中使用的 sql 语句 例:65个查询,包括59个相似查询和51个重复查询) 主要优化这些

image.png
参考文档
https://github.com/jazzband/django-debug-toolbar
https://django-debug-toolbar.readthedocs.io/en/latest/installation.html#getting-the-code
https://github.com/djsutho/django-debug-toolbar-request-history