如何从django调试工具栏中排除某些url?

bvuwiixz  于 2023-03-24  发布在  Go
关注(0)|答案(1)|浏览(119)

我的settings.py文件看起来像-

import os

# This file contains Django settings for lower environments that use the Django Debug Toolbar.
# Currently those envronments are DEV and QA.
from core.settings import *  # noqa: F403

# We need the ability to disable debug_toolbar for regression tests.
DEBUG = os.getenv('DEBUG_MODE', 'FALSE').upper() == 'TRUE'

def toolbar_callback(request):
    return DEBUG

DEBUG_TOOLBAR_CONFIG = {
    "SHOW_TOOLBAR_CALLBACK": toolbar_callback,
}

INSTALLED_APPS += ['debug_toolbar', ]  # noqa: F405
MIDDLEWARE += ['debug_toolbar.middleware.DebugToolbarMiddleware', ]  # noqa: F405

我想从加载调试工具栏排除某些页面。原因是这些页面有很多SQL和调试工具栏加载时间太长,因此页面超时

ui7jx7zq

ui7jx7zq1#

SHOW_TOOLBAR_CALLBACK的值决定了是否应该在页面上使用调试工具栏。因此,您可以通过评估请求路径来排除特定的URL/URL模式。例如,使用生成器表达式:

if DEBUG:
    patterns = [
        "/foo/bar/",
        "/baz/",
    ]
    DEBUG_TOOLBAR_CONFIG = {
        'SHOW_TOOLBAR_CALLBACK': lambda request: not any(p in request.path for p in patterns),
    }

相关问题