You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为何Htmx Ajax触发时Django Debug Toolbar报“已有分析工具激活”错误?

解决Django Debug Toolbar与HTMX共存的Another profiling tool is already active错误

以下是几个可行的解决方案:

方案1:让Debug Toolbar跳过HTMX请求

修改settings.py中的DEBUG_TOOLBAR_CONFIG,通过HTMX自带的请求头判断请求类型,让HTMX请求跳过Debug Toolbar加载,避免profiler冲突:

DEBUG_TOOLBAR_CONFIG = {
    "SHOW_TOOLBAR_CALLBACK": lambda request: not request.META.get('HTTP_HX_REQUEST', False),
}

正常页面请求会保留Toolbar功能,HTMX发起的请求则自动跳过Toolbar加载。

方案2:调整HTMX的触发时机

将hx-trigger="load"改为hx-trigger="DOMContentLoaded",推迟HTMX请求的触发时机,等初始页面的profiler完成后再发起请求:

<div id="group-list" hx-get="{% url "my-rental-property-groups" rental_property.id %}" hx-trigger="DOMContentLoaded"></div>

这个时机比load稍晚,能避开初始请求profiler的活跃周期,同时不需要依赖延迟设置。

方案3:禁用Debug Toolbar的Profiler面板

如果不需要profiler功能,可以直接在settings.py中移除Profiling面板,从根源上消除冲突:

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',
    # 移除Profiling面板
    # 'debug_toolbar.panels.profiling.ProfilingPanel',
]

内容的提问来源于stack exchange,提问作者Moses Machua

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.24 13:07:11