Skip to content

Commit 0200fc1

Browse files
committed
Provide a solution to prevent monkey-patching the settings.
This is necessary to work around extreme cases of circular imports. We really need to reverse URLs at import time and this isn't always possible.
1 parent dc04efa commit 0200fc1

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

debug_toolbar/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from django.utils.importlib import import_module
77

88
import debug_toolbar
9+
from debug_toolbar import settings as dt_settings
910
from debug_toolbar.middleware import DebugToolbarMiddleware
1011

1112

@@ -56,7 +57,7 @@ def patch_root_urlconf():
5657
clear_url_caches()
5758

5859

59-
if settings.DEBUG:
60+
if dt_settings.PATCH_SETTINGS:
6061
patch_internal_ips()
6162
patch_middleware_classes()
6263
patch_root_urlconf()

debug_toolbar/settings.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,6 @@
122122
"%r was renamed to %r. Update your DEBUG_TOOLBAR_PANELS "
123123
"setting." % (old_panel, new_panel), DeprecationWarning)
124124
PANELS[index] = new_panel
125+
126+
127+
PATCH_SETTINGS = getattr(settings, 'DEBUG_TOOLBAR_PATCH_SETTINGS', settings.DEBUG)

docs/configuration.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ settings module to customize its behavior.
1212
it'll prevent you from taking advantage of better defaults that may be
1313
introduced in future releases.
1414

15+
DEBUG_TOOLBAR_PATCH_SETTINGS
16+
----------------------------
17+
18+
This setting defines whether the toolbar will attempt to automatically adjust
19+
your project's settings, as described in the :doc:`installation instructions
20+
<installation>`. By default it has the same value as your ``DEBUG`` setting.
21+
1522
DEBUG_TOOLBAR_PANELS
1623
--------------------
1724

docs/installation.rst

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,21 @@ what it does, or if you prefer defining your settings explicitly, read below.
5151
during the start-up sequence. This works with ``manage.py runserver``
5252
because it validates models before serving requests.
5353

54+
.. warning::
55+
56+
The automatic setup imports your project's URLconf in order to add the
57+
Debug Toolbar's URLs. This may trigger circular imports when the URLconf
58+
imports views that import models. If you're hitting an :exc:`ImportError`,
59+
follow the explicit setup instructions.
60+
5461
Explicit setup
5562
--------------
5663

64+
First, tell the toolbar not to adjust your settings automatically by adding
65+
this line in your settings module::
66+
67+
DEBUG_TOOLBAR_PATCH_SETTINGS = False
68+
5769
URLconf
5870
~~~~~~~
5971

0 commit comments

Comments
 (0)