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