Skip to content

Commit 1062d9c

Browse files
committed
Make it possible for panels to provide URLs and views.
Fix #448.
1 parent f48039e commit 1062d9c

File tree

10 files changed

+57
-25
lines changed

10 files changed

+57
-25
lines changed

debug_toolbar/__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,17 @@
11
from __future__ import unicode_literals
22

3+
34
__all__ = ('VERSION',)
45

6+
57
try:
68
VERSION = __import__('pkg_resources') \
79
.get_distribution('django-debug-toolbar').version
810
except Exception as e:
911
VERSION = 'unknown'
12+
13+
14+
from .toolbar import DebugToolbar
15+
16+
17+
urls = DebugToolbar.get_urls(), 'djdt', 'djdt'

debug_toolbar/models.py

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

8+
import debug_toolbar
89
from debug_toolbar.middleware import DebugToolbarMiddleware
910

1011

@@ -50,7 +51,7 @@ def patch_root_urlconf():
5051
except NoReverseMatch:
5152
urlconf_module = import_module(settings.ROOT_URLCONF)
5253
urlconf_module.urlpatterns += patterns('', # noqa
53-
url(r'^__debug__/', include('debug_toolbar.urls', namespace='djdt', app_name='djdt')),
54+
url(r'^__debug__/', include(debug_toolbar.urls)),
5455
)
5556

5657

debug_toolbar/panels/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ def content(self):
3737
def dom_id(self):
3838
return 'djDebug%sPanel' % (self.name.replace(' ', ''))
3939

40+
# URLs for panel-specific views
41+
42+
@classmethod
43+
def get_urls(cls):
44+
return []
45+
4046
# Titles and subtitles
4147

4248
def nav_title(self):

debug_toolbar/panels/sql.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import uuid
44
from copy import copy
55

6+
from django.conf.urls import patterns, url
67
from django.db import connections
78
from django.utils.translation import ugettext_lazy as _, ungettext_lazy as __
89

@@ -103,6 +104,14 @@ def record(self, alias, **kwargs):
103104
self._sql_time += kwargs['duration']
104105
self._num_queries += 1
105106

107+
@classmethod
108+
def get_urls(cls):
109+
return patterns('debug_toolbar.views', # noqa
110+
url(r'^sql_select/$', 'sql_select', name='sql_select'),
111+
url(r'^sql_explain/$', 'sql_explain', name='sql_explain'),
112+
url(r'^sql_profile/$', 'sql_profile', name='sql_profile'),
113+
)
114+
106115
def nav_title(self):
107116
return _('SQL')
108117

debug_toolbar/panels/template.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import django
77
from django import http
88
from django.conf import settings
9+
from django.conf.urls import patterns, url
910
from django.db.models.query import QuerySet, RawQuerySet
1011
from django.template.context import get_standard_processors
1112
from django.test.signals import template_rendered
@@ -113,6 +114,12 @@ def _store_template_info(self, sender, **kwargs):
113114
kwargs['context'] = [force_text(item) for item in context_list]
114115
self.templates.append(kwargs)
115116

117+
@classmethod
118+
def get_urls(cls):
119+
return patterns('debug_toolbar.views', # noqa
120+
url(r'^template_source/$', 'template_source', name='template_source'),
121+
)
122+
116123
def nav_title(self):
117124
return _('Templates')
118125

debug_toolbar/toolbar.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from __future__ import unicode_literals
66

77
from django.conf import settings
8+
from django.conf.urls import patterns, url
89
from django.core.exceptions import ImproperlyConfigured
910
from django.template.loader import render_to_string
1011
from django.utils.datastructures import SortedDict
@@ -107,3 +108,19 @@ def get_panel_classes(cls):
107108
panel_classes.append(panel_class)
108109
cls._panel_classes = panel_classes
109110
return cls._panel_classes
111+
112+
_urlpatterns = None
113+
114+
@classmethod
115+
def get_urls(cls):
116+
if cls._urlpatterns is None:
117+
# Load URLs in a temporary variable for thread safety.
118+
# Global URLs
119+
urlpatterns = patterns('debug_toolbar.views', # noqa
120+
url(r'^render_panel/$', 'render_panel', name='render_panel'),
121+
)
122+
# Per-panel URLs
123+
for panel_class in cls.get_panel_classes():
124+
urlpatterns += panel_class.get_urls()
125+
cls._urlpatterns = urlpatterns
126+
return cls._urlpatterns

debug_toolbar/urls.py

Lines changed: 0 additions & 18 deletions
This file was deleted.

docs/installation.rst

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,14 @@ Add the Debug Toolbar's URLs to your project's URLconf as follows::
5757
from django.conf.urls import include, patterns, url
5858

5959
if settings.DEBUG:
60+
import debug_toolbar
6061
urlpatterns += patterns('',
61-
url(r'^__debug__/', include('debug_toolbar.urls', namespace='djdt', app_name='djdt')),
62+
url(r'^__debug__/', include(debug_toolbar.urls)),
6263
)
6364

6465
This example uses the ``__debug__`` prefix, but you can use any prefix that
65-
doesn't clash with your application's URLs.
66-
67-
It is mandatory to use the ``djdt`` namespace as shown above. This avoids
68-
conflicts when reversing URLs by name.
66+
doesn't clash with your application's URLs. Note the lack of quotes around
67+
``debug_toolbar.urls``.
6968

7069
If the URLs aren't included in your root URLconf, the Debug Toolbar
7170
automatically appends them.

tests/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ def update_toolbar_panels(**kwargs):
2020
if kwargs['setting'] == 'DEBUG_TOOLBAR_PANELS':
2121
dt_settings.PANELS = kwargs['value'] or dt_settings.PANELS_DEFAULTS
2222
DebugToolbar._panel_classes = None
23+
# Not implemented: invalidate debug_toolbar.urls

tests/urls.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
from django.conf.urls import include, patterns, url
66
from django.contrib import admin
77

8+
import debug_toolbar
9+
810
from .models import NonAsciiRepr
911

1012

@@ -18,5 +20,5 @@
1820
url(r'^non_ascii_request/$', 'regular_view', {'title': NonAsciiRepr()}),
1921
url(r'^new_user/$', 'new_user'),
2022
url(r'^execute_sql/$', 'execute_sql'),
21-
url(r'^__debug__/', include('debug_toolbar.urls', namespace='djdt', app_name='djdt')),
23+
url(r'^__debug__/', include(debug_toolbar.urls)),
2224
)

0 commit comments

Comments
 (0)