1 from debug_toolbar.panels import DebugPanel
2 from django.conf import settings
3 from django.dispatch import dispatcher
4 from django.core.signals import request_started
5 from django.test.signals import template_rendered
6 from django.template.loader import render_to_string
8 # Based on http://www.djangosnippets.org/snippets/766/
9 from django.test.utils import instrumented_test_render
10 from django.template import Template, Context
11 if Template.render != instrumented_test_render:
12 Template.original_render = Template.render
13 Template.render = instrumented_test_render
14 # MONSTER monkey-patch
15 old_template_init = Template.__init__
16 def new_template_init(self, template_string, origin=None, name='<Unknown Template>'):
17 old_template_init(self, template_string, origin, name)
19 Template.__init__ = new_template_init
21 class TemplatesDebugPanel(DebugPanel):
23 Panel that displays information about the SQL queries run while processing the request.
27 def __init__(self, *args, **kwargs):
28 super(TemplatesDebugPanel, self).__init__(*args, **kwargs)
29 self.templates_used = []
30 self.contexts_used = []
31 template_rendered.connect(self._storeRenderedTemplates)
33 def _storeRenderedTemplates(self, **kwargs):
34 template = kwargs.pop('template')
36 self.templates_used.append(template)
37 context = kwargs.pop('context')
39 self.contexts_used.append(context)
41 def process_response(self, request, response):
43 (t.name, t.origin and t.origin.name or 'No origin')
44 for t in self.templates_used
49 return 'Templates: %.2fms'
56 'templates': self.templates,
57 'template_dirs': settings.TEMPLATE_DIRS,
60 return render_to_string('debug_toolbar/panels/templates.html', context)