7
7
from django .template .loader import render_to_string
8
8
from django .test .signals import template_rendered
9
9
from django .utils .translation import ugettext_lazy as _
10
+ from debug_toolbar .middleware import DebugToolbarMiddleware
10
11
from debug_toolbar .panels import DebugPanel
11
12
12
13
# Code taken and adapted from Simon Willison and Django Snippets:
@@ -40,15 +41,15 @@ class TemplateDebugPanel(DebugPanel):
40
41
"""
41
42
name = 'Template'
42
43
has_content = True
43
-
44
+
44
45
def __init__ (self , * args , ** kwargs ):
45
46
super (self .__class__ , self ).__init__ (* args , ** kwargs )
46
47
self .templates = []
47
48
template_rendered .connect (self ._store_template_info )
48
-
49
+
49
50
def _store_template_info (self , sender , ** kwargs ):
50
51
context_data = kwargs ['context' ]
51
-
52
+
52
53
context_list = []
53
54
for context_layer in context_data .dicts :
54
55
temp_layer = {}
@@ -74,22 +75,22 @@ def _store_template_info(self, sender, **kwargs):
74
75
pass
75
76
kwargs ['context' ] = context_list
76
77
self .templates .append (kwargs )
77
-
78
+
78
79
def nav_title (self ):
79
80
return _ ('Templates' )
80
-
81
+
81
82
def title (self ):
82
83
num_templates = len ([t for t in self .templates
83
84
if not (t ['template' ].name and t ['template' ].name .startswith ('debug_toolbar/' ))])
84
85
return _ ('Templates (%(num_templates)s rendered)' ) % {'num_templates' : num_templates }
85
-
86
+
86
87
def url (self ):
87
88
return ''
88
-
89
+
89
90
def process_request (self , request ):
90
91
self .request = request
91
-
92
- def content (self ):
92
+
93
+ def process_response (self , request , response ):
93
94
context_processors = dict (
94
95
[
95
96
("%s.%s" % (k .__module__ , k .__name__ ),
@@ -116,12 +117,17 @@ def content(self):
116
117
context_list = template_data .get ('context' , [])
117
118
info ['context' ] = '\n ' .join (context_list )
118
119
template_context .append (info )
119
-
120
- context = self .context .copy ()
121
- context .update ({
120
+
121
+ self .stats = {
122
122
'templates' : template_context ,
123
123
'template_dirs' : [normpath (x ) for x in settings .TEMPLATE_DIRS ],
124
124
'context_processors' : context_processors ,
125
- })
126
-
125
+ }
126
+
127
+ toolbar = DebugToolbarMiddleware .get_current ()
128
+ toolbar .stats ['template' ] = self .stats
129
+
130
+ def content (self ):
131
+ context = self .context .copy ()
132
+ context .update (self .stats )
127
133
return render_to_string ('debug_toolbar/panels/templates.html' , context )
0 commit comments