|
13 | 13 |
|
14 | 14 | from django.conf import settings
|
15 | 15 | from django.utils.encoding import force_text
|
| 16 | +from django.utils import six |
16 | 17 |
|
17 | 18 | from debug_toolbar.toolbar import DebugToolbar
|
18 | 19 | from debug_toolbar import settings as dt_settings
|
@@ -42,13 +43,20 @@ class DebugToolbarMiddleware(object):
|
42 | 43 | """
|
43 | 44 | debug_toolbars = {}
|
44 | 45 |
|
| 46 | + def __init__(self): |
| 47 | + # If SHOW_TOOLBAR_CALLBACK is a string, which is the recommended |
| 48 | + # setup, resolve it to the corresponding callable. |
| 49 | + func_or_path = dt_settings.CONFIG['SHOW_TOOLBAR_CALLBACK'] |
| 50 | + if isinstance(func_or_path, six.string_types): |
| 51 | + # Replace this with import_by_path in Django >= 1.6. |
| 52 | + mod_path, func_name = func_or_path.rsplit('.', 1) |
| 53 | + self.show_toolbar = getattr(import_module(mod_path), func_name) |
| 54 | + else: |
| 55 | + self.show_toolbar = func_or_path |
| 56 | + |
45 | 57 | def process_request(self, request):
|
46 | 58 | # Decide whether the toolbar is active for this request.
|
47 |
| - func_path = dt_settings.CONFIG['SHOW_TOOLBAR_CALLBACK'] |
48 |
| - # Replace this with import_by_path in Django >= 1.6. |
49 |
| - mod_path, func_name = func_path.rsplit('.', 1) |
50 |
| - show_toolbar = getattr(import_module(mod_path), func_name) |
51 |
| - if not show_toolbar(request): |
| 59 | + if not self.show_toolbar(request): |
52 | 60 | return
|
53 | 61 |
|
54 | 62 | toolbar = DebugToolbar(request)
|
|
0 commit comments