使用Django Debug Toolbar 调试 django rest framework 接口

主要应用

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

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 模板标签除了几个常用的,还真心没有仔细了解一下,看到2.0发布后,翻译学习一下。 本文尽量忠实原著,毕竟大神的东西...
    海明_fd17阅读 6,273评论 0 5
  • PythonWeb框架要点、Django介绍、工程搭建、配置、静态文件与路由 1.Python Web 框架要点 ...
    Cestine阅读 5,765评论 0 6
  • 点我查看本文集的说明及目录。 本项目相关内容包括: 实现过程: CH7 创建在线商店 CH8 管理支付和订单 CH...
    学以致用123阅读 9,145评论 0 6
  • 13 上线 上一章中,你为你的项目创建了RESTful API。在本章中,你会学习以下知识点: 配置一个生产环境 ...
    lakerszhy阅读 5,441评论 1 6
  • 4 创建一个社交网站 在上一章中,你学习了如何创建站点地图和订阅,并且为博客应用构建了一个搜索引擎。在这一章中,你...
    lakerszhy阅读 6,561评论 0 7

友情链接更多精彩内容