在django中使用django_debug_toolbar
一、概述
django_debug_toolbar 是django的第三方工具包,給django擴展了調試功能。
包括查看執行的sql語句,db查詢次數,request,headers,調試概覽等。
二、安裝
使用django_debug_toolbar工具先使用pip安裝。
pip install django_debug_toolbar,然后修改settings.py和urls.py文件。
三、修改settings文件
1. 顯示設置調試工具不要調整settings中的設置
DEBUG_TOOLBAR_PATCH_SETTINGS = False
1
2. 添加調試工具App
INSTALLED_APPS = INSTALLED_APPS + (
'debug_toolbar.apps.DebugToolbarConfig',
)
1
2
3
4
3. 添加調試工具中間件
MIDDLEWARE_CLASSES = MIDDLEWARE_CLASSES + (
'debug_toolbar.middleware.DebugToolbarMiddleware',
)
1
2
3
4. 添加調試工具的IP
INTERNAL_IPS = ("127.0.0.1",)
1
5. debug_toolbar 組件選項
默認值為如下12個組件,可根據需要自行調整。此處不寫代表使用默認值。
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',
]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
下圖顯示了所有推薦組件:
6. debug_toolbar 配置項
默認為如下選項,此處不寫代表使用默認值,可根據需要自行調整。
備注:'JQUERY_URL': '//cdn.bootcss.com/jquery/2.1.4/jquery.min.js'此項原本為google指向的一個js,改成這樣就不會報404了。
CONFIG_DEFAULTS = {
# Toolbar options
'DISABLE_PANELS': {'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,
'SKIP_TEMPLATE_PREFIXES': (
'django/forms/widgets/',
'admin/widgets/',
),
'SQL_WARNING_THRESHOLD': 500, # milliseconds
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
四、修改urls文件
7. debug_toolbar添加到全局url
if settings.DEBUG:
import debug_toolbar
urlpatterns += patterns('', url(r'^__debug__/', include(debug_toolbar.urls)),)
1
2
3
五、非默認Panle和第三方Panle
參考:http://django-debug-toolbar.readthedocs.io/en/1.0/panels.html#non-default-built-in-panels
8. 默認面板
# 查看視圖函數的信息
debug_toolbar.panels.profiling.ProfilingPanel
1
2
9. 第三方面板
注意,第三方面板沒有官方維護!同時,想要使用某個面板,所在的github主頁上查看調用和配置方法。
# 查看您的Haystack后端所做的查詢
haystack_panel.panel.HaystackDebugPanel
# 驗證您的HTML并顯示警告和錯誤
debug_toolbar_htmltidy.panels.HTMLTidyDebugPanel
# 使用調試語句檢索并顯示您指定的信息。Inspector面板也會默認登錄到控制臺
inspector_panel.panels.inspector.InspectorPanel
# 提供了一個profiling panel,它包含了line_profiler的輸出
debug_toolbar_line_profiler.panel.ProfilingPanel
# 跟蹤memcached的使用情況。它目前支持pylibmc和memcache庫
memcache_toolbar.panels.memcache.MemcachePanel或memcache_toolbar.panels.pylibmc.PylibmcPanel
# 添加MongoDB調試信息
debug_toolbar_mongo.panel.MongoDebugPanel
# 在你的django應用程序中跟蹤neo4j rest API調用,這也適用于neo4django和neo4jrestclient
neo4j_panel.Neo4jPanel
# 瀏覽在django.contrib.sites中注冊的網站并在它們之間切換。用于調試使用動態設置的SITE_ID的django-dynamicsites項目。
sites_toolbar.panels.SitesDebugPanel
# 顯示您的Django應用程序的模板渲染時間
template_timings_panel.panels.TemplateTimings.TemplateTimings
# 輕松切換登錄用戶,查看當前用戶的屬性
debug_toolbar_user_panel.panels.UserPanel
---------------------
作者:Nick_Spider
來源:CSDN
原文:https://blog.csdn.net/weixin_39198406/article/details/78821677
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!
轉載于:https://www.cnblogs.com/dalaoban/p/10151842.html
總結
以上是生活随笔為你收集整理的在django中使用django_debug_toolbar的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Androidstudio SVN安装与
- 下一篇: 我马上会重新利用这个博客的