欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Django调试利器django-debug-toolbar

程序员文章站 2022-05-13 21:37:24
...
  • 环境 Python3.5 Django1.11 Django debug toolbar1.8
  • 1 安装:
    $ pip3 install django-debug-toolbar
  • 2 settings.py 配置
INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.humanize',
     ...
    'debug_toolbar'

]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'duser.duser_middleware.URLMiddleware',
    'debug_toolbar.middleware.DebugToolbarMiddleware' # 添加的
]
  • 3 全局urls.py
from django.conf import settings
if settings.DEBUG:
    import debug_toolbar
    urlpatterns.append(url(r'^__debug__/', include(debug_toolbar.urls)))

  • 通常就OK了, 但是需要替换一个js源
    在python的site-packages找到debug_toolbar包,编辑settings.py

CONFIG_DEFAULTS = {
# Toolbar options
'DISABLE_PANELS': set(['debug_toolbar.panels.redirects.RedirectsPanel']),
'INSERT_BEFORE': '</body>',
'JQUERY_URL': '//cdn.bootcss.com/jquery/2.1.4/jquery.min.js', #替换成这样就好了~~
'RENDER_PANELS': None,
'RESULTS_CACHE_SIZE': 10,
'ROOT_TAG_EXTRA_ATTRS': '',
'SHOW_COLLAPSED': False,
'SHOW_TOOLBAR_CALLBACK': 'debug_toolbar.middleware.show_toolbar',
# Panel options
'EXTRA_SIGNALS': [],
'ENABLE_STACKTRACES': True,
'HIDE_IN_STACKTRACES': (
    'socketserver' if six.PY3 else 'SocketServer',
    'threading',
    'wsgiref',
    'debug_toolbar',
    'django',
),
'PROFILER_MAX_DEPTH': 10,
'SHOW_TEMPLATE_CONTEXT': True,
'SQL_WARNING_THRESHOLD': 500,   # milliseconds
DEBUG=False
相关标签: Django-调试