配置流程
大概的配置流程官网上已经很清楚了,主要注意的有2点:(1)'JQUERY_URL'的配置(建议)(2)debug=True模式下的template必须包含closing的(必须)。下面简要介绍下流程:
-
pip install django-debug-toolbar
-
项目的settings.py中
INSTALLED_APPS = [ 'django.contrib.admin', 'django.contrib.auth', 'django.contrib.contenttypes', 'django.contrib.sessions', 'django.contrib.messages', 'django.contrib.staticfiles', 'testapp', 'debug_toolbar', #包含此项 ]
-
项目的urls.py
from django.contrib import admin
from django.urls import include
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
path('testapp/',include('testapp.urls'))
]
###########加入下面的设置###################
from django.conf import settings
from django.urls import include, path
if settings.DEBUG:
import debug_toolbar
urlpatterns = [
path('__debug__/', include(debug_toolbar.urls)),
] + urlpatterns
- 项目settings.py的中间件
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',
'debug_toolbar.middleware.DebugToolbarMiddleware', #加入此项
]
- 项目settings.py中加入:
INTERNAL_IPS = [
'127.0.0.1',
]
- 项目settings.py加入(建议,非必须)
DEBUG_TOOLBAR_CONFIG = {
'JQUERY_URL': 'https://cdn.bootcss.com/jquery/2.2.4/jquery.min.js',
}
- template中必须包含closing ,否则 tool bar 不显示:
<!DOCTYPE html>
<html>
<head>Test Debug</head>
<body>
Just for the purpose of Debugging!
</body>
</html>
参考地址:https://django-debug-toolbar.readthedocs.io/en/latest/tips.html
效果:
还需要注意的是如果在项目settings.py不设置如下:
DEBUG_TOOLBAR_PANELS = [
'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',
]
则toolbar默认全部显示,若只定义部分,则toolbar显示定义的那部分。