【问题标题】:Django-Debug-Toolbar is not showingDjango-Debug-Toolbar 未显示
【发布时间】:2021-07-15 22:09:48
【问题描述】:

我正在构建 BlogApp,我正在尝试运行 Django-Debug-Toolbar 但它没有显示。我已经看到了很多答案,但对我没有任何帮助。

我已经按照Documentation正确安装了

我添加了installed apps、middlewares 和urls 以及collecstatic。但是当我转到浏览器时仍然没有显示。

settings.py

if DEBUG:
    MIDDLEWARE += [
        'debug_toolbar.middleware.DebugToolbarMiddleware',
    ]
    INSTALLED_APPS += [
        'debug_toolbar',
    ]
    INTERNAL_IPS = ['127.0.0.1', ]

    # this is the main reason for not showing up the toolbar
    import mimetypes
    mimetypes.add_type("application/javascript", ".js", True)

    DEBUG_TOOLBAR_CONFIG = {
        'INTERCEPT_REDIRECTS': False,
    }

urls.py

if settings.DEBUG:
    import debug_toolbar
    urlpatterns += [
        path('__debug__/', include(debug_toolbar.urls)),
    ]

任何帮助将不胜感激。

提前致谢

【问题讨论】:

  • 我可以想象两件事,首先,你能在你的项目中得到任何静态文件吗?可能STATIC_URL 配置不正确。第二个假设是DEBUG_TOOLBAR_CONFIG 中的SHOW_TOOLBAR_CALLBACK 变量,您能否将其更改为True?
  • 我也这样做了。但仍然没有显示。当我签入我的服务器时,它正在正确加载debug_toolbar/css/toolbar.css。和static path is configured correctly.
  • 默认情况下,工具栏的 HTML 被注入到正文的末尾。如果您的模板不包含结束 </body> 标记,它将不会被注入。

标签: python django debugging django-debug-toolbar


【解决方案1】:

我将这段代码放在设置的底部并解决了我的问题:

settings.py

import mimetypes
mimetypes.add_type("application/javascript", ".js", True)

DEBUG_TOOLBAR_PATCH_SETTINGS = False

def show_toolbar(request):
    return True
    
DEBUG_TOOLBAR_CONFIG = {
'INTERCEPT_REDIRECTS': False,
"SHOW_TOOLBAR_CALLBACK": show_toolbar,
'INSERT_BEFORE': '</head>',
'INTERCEPT_REDIRECTS': False,
'RENDER_PANELS': True,
}

我遇到的另一个问题是 my_project/static/ 文件中没有 debug_tolbar/css 文件。 Collectstatic 没有解决我的问题。 解决方案:我从:python3.8/site-packages/debug_toolbar/static/debug_toolbar/css/复制到my_project/static/debug_toolbar/css/

【讨论】:

    猜你喜欢
    • 2022-08-13
    • 2022-06-28
    • 2021-11-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 2014-05-10
    • 2015-05-13
    相关资源
    最近更新 更多