Skip to content

Commit 7f7ea81

Browse files
committed
Fixed the toolbar loading mechanism to only happen when the middleware has been added to the MIDDLEWARE_CLASSES setting.
1 parent 41e26d8 commit 7f7ea81

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

debug_toolbar/models.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,30 @@
1+
from django.conf import settings
2+
from django.utils.importlib import import_module
3+
14
from debug_toolbar.toolbar.loader import load_panel_classes
5+
from debug_toolbar.middleware import DebugToolbarMiddleware
6+
7+
loaded = False
8+
9+
10+
def is_toolbar(cls):
11+
return (issubclass(cls, DebugToolbarMiddleware) or
12+
DebugToolbarMiddleware in getattr(cls, '__bases__', ()))
13+
14+
15+
def iter_toolbar_middlewares():
16+
global loaded
17+
for middleware_path in settings.MIDDLEWARE_CLASSES:
18+
try:
19+
mod_path, cls_name = middleware_path.rsplit('.', 1)
20+
mod = import_module(mod_path)
21+
middleware_cls = getattr(mod, cls_name)
22+
except (AttributeError, ImportError, ValueError):
23+
continue
24+
if is_toolbar(middleware_cls) and not loaded:
25+
# we have a hit!
26+
loaded = True
27+
yield middleware_cls
228

3-
load_panel_classes()
29+
for middleware_cls in iter_toolbar_middlewares():
30+
load_panel_classes()

0 commit comments

Comments
 (0)