@@ -83,58 +83,11 @@ def _store_template_info(self, sender, **kwargs):
83
83
if is_debug_toolbar_template :
84
84
return
85
85
86
- context_list = []
87
- for context_layer in context .dicts :
88
- if hasattr (context_layer , "items" ) and context_layer :
89
- # Check if the layer is in the cache.
90
- pformatted = None
91
- for key_values , _pformatted in self .pformat_layers :
92
- if key_values == context_layer :
93
- pformatted = _pformatted
94
- break
95
-
96
- if pformatted is None :
97
- temp_layer = {}
98
- for key , value in context_layer .items ():
99
- # Replace any request elements - they have a large
100
- # Unicode representation and the request data is
101
- # already made available from the Request panel.
102
- if isinstance (value , http .HttpRequest ):
103
- temp_layer [key ] = "<<request>>"
104
- # Replace the debugging sql_queries element. The SQL
105
- # data is already made available from the SQL panel.
106
- elif key == "sql_queries" and isinstance (value , list ):
107
- temp_layer [key ] = "<<sql_queries>>"
108
- # Replace LANGUAGES, which is available in i18n context
109
- # processor
110
- elif key == "LANGUAGES" and isinstance (value , tuple ):
111
- temp_layer [key ] = "<<languages>>"
112
- # QuerySet would trigger the database: user can run the
113
- # query from SQL Panel
114
- elif isinstance (value , (QuerySet , RawQuerySet )):
115
- temp_layer [key ] = "<<{} of {}>>" .format (
116
- value .__class__ .__name__ .lower (),
117
- value .model ._meta .label ,
118
- )
119
- else :
120
- token = allow_sql .set (False ) # noqa: FBT003
121
- try :
122
- saferepr (value ) # this MAY trigger a db query
123
- except SQLQueryTriggered :
124
- temp_layer [key ] = "<<triggers database query>>"
125
- except UnicodeEncodeError :
126
- temp_layer [key ] = "<<Unicode encode error>>"
127
- except Exception :
128
- temp_layer [key ] = "<<unhandled exception>>"
129
- else :
130
- temp_layer [key ] = value
131
- finally :
132
- allow_sql .reset (token )
133
- pformatted = pformat (temp_layer )
134
- self .pformat_layers .append ((context_layer , pformatted ))
135
- context_list .append (pformatted )
136
-
137
- kwargs ["context" ] = context_list
86
+ kwargs ["context" ] = [
87
+ context_layer
88
+ for context_layer in context .dicts
89
+ if hasattr (context_layer , "items" ) and context_layer
90
+ ]
138
91
kwargs ["context_processors" ] = getattr (context , "context_processors" , None )
139
92
self .templates .append (kwargs )
140
93
@@ -167,6 +120,64 @@ def enable_instrumentation(self):
167
120
def disable_instrumentation (self ):
168
121
template_rendered .disconnect (self ._store_template_info )
169
122
123
+ def process_context_list (self , context_layers ):
124
+ context_list = []
125
+ for context_layer in context_layers :
126
+ # Check if the layer is in the cache.
127
+ pformatted = None
128
+ for key_values , _pformatted in self .pformat_layers :
129
+ if key_values == context_layer :
130
+ pformatted = _pformatted
131
+ break
132
+
133
+ if pformatted is None :
134
+ temp_layer = {}
135
+ for key , value in context_layer .items ():
136
+ # Do not force evaluating LazyObject
137
+ if hasattr (value , "_wrapped" ):
138
+ # SimpleLazyObject has __repr__ which includes actual value
139
+ # if it has been already evaluated
140
+ temp_layer [key ] = repr (value )
141
+ # Replace any request elements - they have a large
142
+ # Unicode representation and the request data is
143
+ # already made available from the Request panel.
144
+ elif isinstance (value , http .HttpRequest ):
145
+ temp_layer [key ] = "<<request>>"
146
+ # Replace the debugging sql_queries element. The SQL
147
+ # data is already made available from the SQL panel.
148
+ elif key == "sql_queries" and isinstance (value , list ):
149
+ temp_layer [key ] = "<<sql_queries>>"
150
+ # Replace LANGUAGES, which is available in i18n context
151
+ # processor
152
+ elif key == "LANGUAGES" and isinstance (value , tuple ):
153
+ temp_layer [key ] = "<<languages>>"
154
+ # QuerySet would trigger the database: user can run the
155
+ # query from SQL Panel
156
+ elif isinstance (value , (QuerySet , RawQuerySet )):
157
+ temp_layer [key ] = "<<{} of {}>>" .format (
158
+ value .__class__ .__name__ .lower (),
159
+ value .model ._meta .label ,
160
+ )
161
+ else :
162
+ token = allow_sql .set (False ) # noqa: FBT003
163
+ try :
164
+ saferepr (value ) # this MAY trigger a db query
165
+ except SQLQueryTriggered :
166
+ temp_layer [key ] = "<<triggers database query>>"
167
+ except UnicodeEncodeError :
168
+ temp_layer [key ] = "<<Unicode encode error>>"
169
+ except Exception :
170
+ temp_layer [key ] = "<<unhandled exception>>"
171
+ else :
172
+ temp_layer [key ] = value
173
+ finally :
174
+ allow_sql .reset (token )
175
+ pformatted = pformat (temp_layer )
176
+ self .pformat_layers .append ((context_layer , pformatted ))
177
+ context_list .append (pformatted )
178
+
179
+ return context_list
180
+
170
181
def generate_stats (self , request , response ):
171
182
template_context = []
172
183
for template_data in self .templates :
@@ -182,8 +193,11 @@ def generate_stats(self, request, response):
182
193
info ["template" ] = template
183
194
# Clean up context for better readability
184
195
if self .toolbar .config ["SHOW_TEMPLATE_CONTEXT" ]:
185
- context_list = template_data .get ("context" , [])
186
- info ["context" ] = "\n " .join (context_list )
196
+ if "context_list" not in template_data :
197
+ template_data ["context_list" ] = self .process_context_list (
198
+ template_data .get ("context" , [])
199
+ )
200
+ info ["context" ] = "\n " .join (template_data ["context_list" ])
187
201
template_context .append (info )
188
202
189
203
# Fetch context_processors/template_dirs from any template
0 commit comments