Skip to content

Commit 8845345

Browse files
Create urls module and remove import package from docs. (#1537)
* Create urls module and remove import package from docs. By creating a urls module, devs can include the urls with a string path to the urls, include('debug_toolbar.urls'). * Instantiate each panel when DebugToolbarConfig is ready. This allows the cache panel to enable the instrumentation when the app is ready. * Support previous urls installation. Update the __init__.py module to use the proper urls string path and app name. This will maintain backwards compatability with the documentations previous installation instructions. * Add test for old style, debug_toolbar.urls usage.
1 parent ffa60c2 commit 8845345

File tree

10 files changed

+46
-13
lines changed

10 files changed

+46
-13
lines changed

debug_toolbar/__init__.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import django
22

3+
from debug_toolbar.urls import app_name
4+
35
__all__ = ["VERSION"]
46

57

@@ -9,7 +11,7 @@
911

1012
# Code that discovers files or modules in INSTALLED_APPS imports this module.
1113

12-
urls = "debug_toolbar.toolbar", "djdt"
14+
urls = "debug_toolbar.urls", app_name
1315

1416
if django.VERSION < (3, 2):
1517
default_app_config = "debug_toolbar.apps.DebugToolbarConfig"

debug_toolbar/apps.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ class DebugToolbarConfig(AppConfig):
1414
name = "debug_toolbar"
1515
verbose_name = _("Debug Toolbar")
1616

17+
def ready(self):
18+
from debug_toolbar.toolbar import DebugToolbar
19+
20+
# Import the panels when the app is ready. This allows panels
21+
# like CachePanel to enable the instrumentation immediately.
22+
DebugToolbar.get_panel_classes()
23+
1724

1825
@register
1926
def check_middleware(app_configs, **kwargs):

debug_toolbar/toolbar.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,8 @@ def is_toolbar_request(cls, request):
143143
"""
144144
Determine if the request is for a DebugToolbar view.
145145
"""
146+
from debug_toolbar.urls import app_name
147+
146148
# The primary caller of this function is in the middleware which may
147149
# not have resolver_match set.
148150
try:
@@ -152,7 +154,3 @@ def is_toolbar_request(cls, request):
152154
except Resolver404:
153155
return False
154156
return resolver_match.namespaces and resolver_match.namespaces[-1] == app_name
155-
156-
157-
app_name = "djdt"
158-
urlpatterns = DebugToolbar.get_urls()

debug_toolbar/urls.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from debug_toolbar.toolbar import DebugToolbar
2+
3+
app_name = "djdt"
4+
urlpatterns = DebugToolbar.get_urls()

docs/changes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Next version
1111
usages before ``enable_instrumentation`` is called.
1212
* Add check ``W006`` to warn that the toolbar is incompatible with
1313
``TEMPLATES`` settings configurations with ``APP_DIRS`` set to ``False``.
14+
* Create ``urls`` module and update documentation to no longer require
15+
importing the toolbar package.
1416

1517

1618
3.2.2 (2021-08-14)

docs/installation.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,11 @@ Add django-debug-toolbar's URLs to your project's URLconf:
7979

8080
.. code-block:: python
8181
82-
import debug_toolbar
8382
from django.urls import include, path
8483
8584
urlpatterns = [
8685
# ...
87-
path('__debug__/', include(debug_toolbar.urls)),
86+
path('__debug__/', include('debug_toolbar.urls')),
8887
]
8988
9089
This example uses the ``__debug__`` prefix, but you can use any prefix that

example/urls.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22
from django.urls import include, path
33
from django.views.generic import TemplateView
44

5-
import debug_toolbar
6-
75
urlpatterns = [
86
path("", TemplateView.as_view(template_name="index.html")),
97
path("jquery/", TemplateView.as_view(template_name="jquery/index.html")),
108
path("mootools/", TemplateView.as_view(template_name="mootools/index.html")),
119
path("prototype/", TemplateView.as_view(template_name="prototype/index.html")),
1210
path("admin/", admin.site.urls),
13-
path("__debug__/", include(debug_toolbar.urls)),
11+
path("__debug__/", include("debug_toolbar.urls")),
1412
]

tests/test_integration.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,20 @@ def get_response(request):
101101
self.assertContains(response, "</div>\n</body>")
102102

103103
def test_cache_page(self):
104+
# Clear the cache before testing the views. Other tests that use cached_view
105+
# may run earlier and cause fewer cache calls.
106+
cache.clear()
107+
self.client.get("/cached_view/")
108+
self.assertEqual(len(self.toolbar.get_panel_by_id("CachePanel").calls), 3)
109+
self.client.get("/cached_view/")
110+
self.assertEqual(len(self.toolbar.get_panel_by_id("CachePanel").calls), 5)
111+
112+
@override_settings(ROOT_URLCONF="tests.urls_use_package_urls")
113+
def test_include_package_urls(self):
114+
"""Test urlsconf that uses the debug_toolbar.urls in the include call"""
115+
# Clear the cache before testing the views. Other tests that use cached_view
116+
# may run earlier and cause fewer cache calls.
117+
cache.clear()
104118
self.client.get("/cached_view/")
105119
self.assertEqual(len(self.toolbar.get_panel_by_id("CachePanel").calls), 3)
106120
self.client.get("/cached_view/")

tests/urls.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
from django.contrib.auth.views import LoginView
33
from django.urls import include, path, re_path
44

5-
import debug_toolbar
6-
75
from . import views
86
from .models import NonAsciiRepr
97

@@ -25,5 +23,5 @@
2523
path("redirect/", views.redirect_view),
2624
path("login_without_redirect/", LoginView.as_view(redirect_field_name=None)),
2725
path("admin/", admin.site.urls),
28-
path("__debug__/", include(debug_toolbar.urls)),
26+
path("__debug__/", include("debug_toolbar.urls")),
2927
]

tests/urls_use_package_urls.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
"""urls.py to test using debug_toolbar.urls in include"""
2+
from django.urls import include, path
3+
4+
import debug_toolbar
5+
6+
from . import views
7+
8+
urlpatterns = [
9+
path("cached_view/", views.cached_view),
10+
path("__debug__/", include(debug_toolbar.urls)),
11+
]

0 commit comments

Comments
 (0)