Skip to content

Commit 325ea19

Browse files
Introduce debug_toolbar_urls to simplify installation (#1926)
While this isn't a huge improvement, it provides a hook for us to more easily introduce library level controls for when urls are installed. It also eliminates the user from having to write an if statement for the most basic of installations.
1 parent 2d9c6a7 commit 325ea19

File tree

5 files changed

+58
-16
lines changed

5 files changed

+58
-16
lines changed

debug_toolbar/toolbar.py

+27-1
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
The main DebugToolbar class that loads and renders the Toolbar.
33
"""
44

5+
import re
56
import uuid
67
from collections import OrderedDict
78
from functools import lru_cache
89

910
from django.apps import apps
11+
from django.conf import settings
1012
from django.core.exceptions import ImproperlyConfigured
1113
from django.dispatch import Signal
1214
from django.template import TemplateSyntaxError
1315
from django.template.loader import render_to_string
14-
from django.urls import path, resolve
16+
from django.urls import include, path, re_path, resolve
1517
from django.urls.exceptions import Resolver404
1618
from django.utils.module_loading import import_string
1719
from django.utils.translation import get_language, override as lang_override
@@ -186,3 +188,27 @@ def observe_request(request):
186188
Determine whether to update the toolbar from a client side request.
187189
"""
188190
return True
191+
192+
193+
def debug_toolbar_urls(prefix="__debug__"):
194+
"""
195+
Return a URL pattern for serving toolbar in debug mode.
196+
197+
from django.conf import settings
198+
from debug_toolbar.toolbar import debug_toolbar_urls
199+
200+
urlpatterns = [
201+
# ... the rest of your URLconf goes here ...
202+
] + debug_toolbar_urls()
203+
"""
204+
if not prefix:
205+
raise ImproperlyConfigured("Empty urls prefix not permitted")
206+
elif not settings.DEBUG:
207+
# No-op if not in debug mode.
208+
return []
209+
return [
210+
re_path(
211+
r"^%s/" % re.escape(prefix.lstrip("/")),
212+
include("debug_toolbar.urls"),
213+
),
214+
]

docs/changes.rst

+2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Pending
99
* Limit ``E001`` check to likely error cases when the
1010
``SHOW_TOOLBAR_CALLBACK`` has changed, but the toolbar's URL
1111
paths aren't installed.
12+
* Introduce helper function ``debug_toolbar_urls`` to
13+
simplify installation.
1214

1315
4.4.2 (2024-05-27)
1416
------------------

docs/installation.rst

+9-7
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,14 @@ Add django-debug-toolbar's URLs to your project's URLconf:
9595
.. code-block:: python
9696
9797
from django.urls import include, path
98+
from debug_toolbar.toolbar import debug_toolbar_urls
9899
99100
urlpatterns = [
100-
# ...
101-
path("__debug__/", include("debug_toolbar.urls")),
102-
]
101+
# ... the rest of your URLconf goes here ...
102+
] + debug_toolbar_urls()
103103
104-
This example uses the ``__debug__`` prefix, but you can use any prefix that
105-
doesn't clash with your application's URLs.
104+
By default this uses the ``__debug__`` prefix for the paths, but you can
105+
use any prefix that doesn't clash with your application's URLs.
106106

107107

108108
5. Add the Middleware
@@ -180,11 +180,13 @@ You should also modify your URLconf file:
180180

181181
.. code-block:: python
182182
183+
from django.conf import settings
184+
from debug_toolbar.toolbar import debug_toolbar_urls
185+
183186
if not settings.TESTING:
184187
urlpatterns = [
185188
*urlpatterns,
186-
path("__debug__/", include("debug_toolbar.urls")),
187-
]
189+
] + debug_toolbar_urls()
188190
189191
Alternatively, you can check out the :ref:`IS_RUNNING_TESTS <IS_RUNNING_TESTS>`
190192
option.

example/urls.py

+3-8
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from django.conf import settings
21
from django.contrib import admin
3-
from django.urls import include, path
2+
from django.urls import path
43
from django.views.generic import TemplateView
54

5+
from debug_toolbar.toolbar import debug_toolbar_urls
66
from example.views import increment
77

88
urlpatterns = [
@@ -34,9 +34,4 @@
3434
),
3535
path("admin/", admin.site.urls),
3636
path("ajax/increment", increment, name="ajax_increment"),
37-
]
38-
39-
if settings.ENABLE_DEBUG_TOOLBAR:
40-
urlpatterns += [
41-
path("__debug__/", include("debug_toolbar.urls")),
42-
]
37+
] + debug_toolbar_urls()

tests/test_toolbar.py

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
from django.core.exceptions import ImproperlyConfigured
2+
3+
from debug_toolbar.toolbar import debug_toolbar_urls
4+
from tests.base import BaseTestCase
5+
6+
7+
class DebugToolbarUrlsTestCase(BaseTestCase):
8+
def test_empty_prefix_errors(self):
9+
with self.assertRaises(ImproperlyConfigured):
10+
debug_toolbar_urls(prefix="")
11+
12+
def test_empty_when_debug_is_false(self):
13+
self.assertEqual(debug_toolbar_urls(), [])
14+
15+
def test_has_path(self):
16+
with self.settings(DEBUG=True):
17+
self.assertEqual(len(debug_toolbar_urls()), 1)

0 commit comments

Comments
 (0)