1
1
from __future__ import unicode_literals
2
2
3
- import os
4
3
import re
5
4
import sys
6
5
import types
10
9
from django .http import (
11
10
HttpRequest , HttpResponse , HttpResponseNotFound , build_request_repr ,
12
11
)
13
- from django .template import Context , Engine , TemplateDoesNotExist
12
+ from django .template import Context , Engine , TemplateDoesNotExist , engines
14
13
from django .template .defaultfilters import force_escape , pprint
15
14
from django .utils import lru_cache , six , timezone
16
15
from django .utils .datastructures import MultiValueDict
@@ -266,61 +265,36 @@ def __init__(self, request, exc_type, exc_value, tb, is_email=False):
266
265
267
266
self .template_info = getattr (self .exc_value , 'template_debug' , None )
268
267
self .template_does_not_exist = False
269
- self .loader_debug_info = None
268
+ self .postmortem = None
270
269
271
270
# Handle deprecated string exceptions
272
271
if isinstance (self .exc_type , six .string_types ):
273
272
self .exc_value = Exception ('Deprecated String Exception: %r' % self .exc_type )
274
273
self .exc_type = type (self .exc_value )
275
274
276
- def format_path_status (self , path ):
277
- if not os .path .exists (path ):
278
- return "File does not exist"
279
- return "File exists"
280
-
281
275
def get_traceback_data (self ):
282
276
"""Return a dictionary containing traceback information."""
283
- try :
284
- default_template_engine = Engine .get_default ()
285
- except Exception :
286
- # Since the debug view must never crash, catch all exceptions.
287
- # If Django can't find a default template engine, get_default()
288
- # raises ImproperlyConfigured. If some template engines fail to
289
- # load, any exception may be raised.
290
- default_template_engine = None
291
-
292
- # TODO: add support for multiple template engines (#24120).
293
- # TemplateDoesNotExist should carry all the information.
294
- # Replaying the search process isn't a good design.
295
277
if self .exc_type and issubclass (self .exc_type , TemplateDoesNotExist ):
296
- if default_template_engine is None :
297
- template_loaders = []
298
- else :
299
- self .template_does_not_exist = True
300
- self .loader_debug_info = []
301
- # If Django fails in get_template_loaders, provide an empty list
302
- # for the following loop to not fail.
303
- try :
304
- template_loaders = default_template_engine .template_loaders
305
- except Exception :
306
- template_loaders = []
307
-
308
- for loader in template_loaders :
309
- try :
310
- source_list_func = loader .get_template_sources
311
- # NOTE: This assumes exc_value is the name of the template that
312
- # the loader attempted to load.
313
- template_list = [{
314
- 'name' : t ,
315
- 'status' : self .format_path_status (t ),
316
- } for t in source_list_func (str (self .exc_value ))]
317
- except AttributeError :
318
- template_list = []
319
- loader_name = loader .__module__ + '.' + loader .__class__ .__name__
320
- self .loader_debug_info .append ({
321
- 'loader' : loader_name ,
322
- 'templates' : template_list ,
323
- })
278
+ self .template_does_not_exist = True
279
+ postmortem = []
280
+
281
+ # TODO: add support for multiple template engines (#24120).
282
+ # TemplateDoesNotExist should carry all the information, including
283
+ # the backend, rather than looping through engines.all.
284
+ for engine in engines .all ():
285
+ if hasattr (engine , 'engine' ):
286
+ e = engine .engine
287
+ else :
288
+ e = engine
289
+
290
+ postmortem .append (dict (
291
+ engine = engine ,
292
+ tried = [
293
+ entry for entry in self .exc_value .tried if
294
+ entry [0 ].loader .engine == e
295
+ ],
296
+ ))
297
+ self .postmortem = postmortem
324
298
325
299
frames = self .get_traceback_frames ()
326
300
for i , frame in enumerate (frames ):
@@ -363,7 +337,7 @@ def get_traceback_data(self):
363
337
'sys_path' : sys .path ,
364
338
'template_info' : self .template_info ,
365
339
'template_does_not_exist' : self .template_does_not_exist ,
366
- 'loader_debug_info ' : self .loader_debug_info ,
340
+ 'postmortem ' : self .postmortem ,
367
341
}
368
342
# Check whether exception info is available
369
343
if self .exc_type :
@@ -634,7 +608,8 @@ def default_urlconf(request):
634
608
#summary h2 { font-weight: normal; color: #666; }
635
609
#explanation { background:#eee; }
636
610
#template, #template-not-exist { background:#f6f6f6; }
637
- #template-not-exist ul { margin: 0 0 0 20px; }
611
+ #template-not-exist ul { margin: 0 0 10px 20px; }
612
+ #template-not-exist .postmortem-section { margin-bottom: 3px; }
638
613
#unicode-hint { background:#eee; }
639
614
#traceback { background:#eee; }
640
615
#requestinfo { background:#f6f6f6; padding-left:120px; }
@@ -646,6 +621,7 @@ def default_urlconf(request):
646
621
h2 span.commands { font-size:.7em;}
647
622
span.commands a:link {color:#5E5694;}
648
623
pre.exception_value { font-family: sans-serif; color: #666; font-size: 1.5em; margin: 10px 0 10px 0; }
624
+ .append-bottom { margin-bottom: 10px; }
649
625
</style>
650
626
{% if not is_email %}
651
627
<script type="text/javascript">
@@ -772,19 +748,23 @@ def default_urlconf(request):
772
748
{% if template_does_not_exist %}
773
749
<div id="template-not-exist">
774
750
<h2>Template-loader postmortem</h2>
775
- {% if loader_debug_info %}
776
- <p>Django tried loading these templates, in this order:</p>
777
- <ul>
778
- {% for loader in loader_debug_info %}
779
- <li>Using loader <code>{{ loader.loader }}</code>:
780
- <ul>
781
- {% for t in loader.templates %}<li><code>{{ t.name }}</code> ({{ t.status }})</li>{% endfor %}
782
- </ul>
783
- </li>
751
+ {% if postmortem %}
752
+ <p class="append-bottom">Django tried loading these templates, in this order:</p>
753
+ {% for entry in postmortem %}
754
+ <p class="postmortem-section">Using engine <code>{{ entry.engine.name }}</code>:</p>
755
+ <ul>
756
+ {% if entry.tried %}
757
+ {% for attempt in entry.tried %}
758
+ <li><code>{{ attempt.0.loader_name }}</code>: {{ attempt.0.name }} ({{ attempt.1 }})</li>
759
+ {% endfor %}
760
+ </ul>
761
+ {% else %}
762
+ <li>This engine did not provide a list of tried templates.</li>
763
+ {% endif %}
764
+ </ul>
784
765
{% endfor %}
785
- </ul>
786
766
{% else %}
787
- <p>Django couldn't find any templates because your <code>'loaders'</code> option is empty! </p>
767
+ <p>No templates were found because your 'TEMPLATES' setting is not configured. </p>
788
768
{% endif %}
789
769
</div>
790
770
{% endif %}
@@ -907,12 +887,14 @@ def default_urlconf(request):
907
887
Installed Middleware:
908
888
{{ settings.MIDDLEWARE_CLASSES|pprint }}
909
889
910
- {% if template_does_not_exist %}Template Loader Error:
911
- {% if loader_debug_info %}Django tried loading these templates, in this order:
912
- {% for loader in loader_debug_info %}Using loader {{ loader.loader }}:
913
- {% for t in loader.templates %}{{ t.name }} ({{ t.status }})
914
- {% endfor %}{% endfor %}
915
- {% else %}Django couldn't find any templates because your 'loaders' option is empty!
890
+ {% if template_does_not_exist %}Template loader postmortem
891
+ {% if postmortem %}Django tried loading these templates, in this order:
892
+ {% for entry in postmortem %}
893
+ Using engine {{ entry.engine.name }}:
894
+ {% if entry.tried %}{% for attempt in entry.tried %} * {{ attempt.0.loader_name }}: {{ attempt.0.name }} ({{ attempt.1 }})
895
+ {% endfor %}{% else %} This engine did not provide a list of tried templates.
896
+ {% endif %}{% endfor %}
897
+ {% else %}No templates were found because your 'TEMPLATES' setting is not configured.
916
898
{% endif %}
917
899
{% endif %}{% if template_info %}
918
900
Template error:
@@ -1098,12 +1080,14 @@ def default_urlconf(request):
1098
1080
{{ settings.INSTALLED_APPS|pprint }}
1099
1081
Installed Middleware:
1100
1082
{{ settings.MIDDLEWARE_CLASSES|pprint }}
1101
- {% if template_does_not_exist %}Template loader Error:
1102
- {% if loader_debug_info %}Django tried loading these templates, in this order:
1103
- {% for loader in loader_debug_info %}Using loader {{ loader.loader }}:
1104
- {% for t in loader.templates %}{{ t.name }} ({{ t.status }})
1105
- {% endfor %}{% endfor %}
1106
- {% else %}Django couldn't find any templates because your 'loaders' option is empty!
1083
+ {% if template_does_not_exist %}Template loader postmortem
1084
+ {% if postmortem %}Django tried loading these templates, in this order:
1085
+ {% for entry in postmortem %}
1086
+ Using engine {{ entry.engine.name }}:
1087
+ {% if entry.tried %}{% for attempt in entry.tried %} * {{ attempt.0.loader_name }}: {{ attempt.0.name }} ({{ attempt.1 }})
1088
+ {% endfor %}{% else %} This engine did not provide a list of tried templates.
1089
+ {% endif %}{% endfor %}
1090
+ {% else %}No templates were found because your 'TEMPLATES' setting is not configured.
1107
1091
{% endif %}
1108
1092
{% endif %}{% if template_info %}
1109
1093
Template error:
0 commit comments