Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #892 -- TemplateDoesNotExist errors now get a 'Template-loader …

…postmortem' section on the pretty debug page. Thanks for the idea, David Ascher

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1400 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit d9228251562f4f37cd76da4404874483718c7728 1 parent 22746d3
Adrian Holovaty authored November 24, 2005

Showing 1 changed file with 43 additions and 4 deletions. Show diff stats Hide diff stats

  1. 47  django/views/debug.py
47  django/views/debug.py
... ...
@@ -1,5 +1,5 @@
1 1
 from django.conf import settings
2  
-from django.core.template import Template, Context
  2
+from django.core.template import Template, Context, TemplateDoesNotExist
3 3
 from django.utils.html import escape
4 4
 from django.utils.httpwrappers import HttpResponseServerError, HttpResponseNotFound
5 5
 import inspect, os, re, sys
@@ -43,8 +43,8 @@ def get_template_exception_info(exc_type, exc_value, tb):
43 43
         'before': before,
44 44
         'during': during,
45 45
         'after': after,
46  
-        'top': top ,
47  
-        'bottom': bottom ,
  46
+        'top': top,
  47
+        'bottom': bottom,
48 48
         'total': total,
49 49
         'line': line,
50 50
         'name': origin.name,
@@ -58,6 +58,25 @@ def technical_500_response(request, exc_type, exc_value, tb):
58 58
     the values returned from sys.exc_info() and friends.
59 59
     """
60 60
     template_info = None
  61
+    template_does_not_exist = False
  62
+    loader_debug_info = None
  63
+    if issubclass(exc_type, TemplateDoesNotExist):
  64
+        from django.core.template.loader import template_source_loaders
  65
+        template_does_not_exist = True
  66
+        loader_debug_info = []
  67
+        for loader in template_source_loaders:
  68
+            try:
  69
+                source_list_func = getattr(__import__(loader.__module__, '', '', ['get_template_sources']), 'get_template_sources')
  70
+                # NOTE: This assumes exc_value is the name of the template that
  71
+                # the loader attempted to load.
  72
+                template_list = [{'name': t, 'exists': os.path.exists(t)} \
  73
+                    for t in source_list_func(str(exc_value))]
  74
+            except (ImportError, AttributeError):
  75
+                template_list = []
  76
+            loader_debug_info.append({
  77
+                'loader': loader.__module__ + '.' + loader.__name__,
  78
+                'templates': template_list,
  79
+            })
61 80
     if settings.TEMPLATE_DEBUG and hasattr(exc_value, 'source'):
62 81
         exc_type, exc_value, tb, template_info = get_template_exception_info(exc_type, exc_value, tb)
63 82
     frames = []
@@ -100,6 +119,8 @@ def technical_500_response(request, exc_type, exc_value, tb):
100 119
         'request_protocol': os.environ.get("HTTPS") == "on" and "https" or "http",
101 120
         'settings': settings_dict,
102 121
         'template_info': template_info,
  122
+        'template_does_not_exist': template_does_not_exist,
  123
+        'loader_debug_info': loader_debug_info,
103 124
     })
104 125
     return HttpResponseServerError(t.render(c), mimetype='text/html')
105 126
 
@@ -187,7 +208,8 @@ def _get_lines_from_file(filename, lineno, context_lines):
187 208
     #summary { background: #ffc; }
188 209
     #summary h2 { font-weight: normal; color: #666; }
189 210
     #explanation { background:#eee; }
190  
-    #template { background:#f6f6f6; }
  211
+    #template, #template-not-exist { background:#f6f6f6; }
  212
+    #template-not-exist ul { margin: 0 0 0 20px; }
191 213
     #traceback { background:#eee; }
192 214
     #requestinfo { background:#f6f6f6; padding-left:120px; }
193 215
     #summary table { border:none; background:transparent; }
@@ -272,6 +294,23 @@ def _get_lines_from_file(filename, lineno, context_lines):
272 294
     </tr>
273 295
   </table>
274 296
 </div>
  297
+{% if template_does_not_exist %}
  298
+<div id="template-not-exist">
  299
+    <h2>Template-loader postmortem</h2>
  300
+    {% if loader_debug_info %}
  301
+        <p>Django tried loading these templates, in this order:</p>
  302
+        <ul>
  303
+        {% for loader in loader_debug_info %}
  304
+            <li>Using loader <code>{{ loader.loader }}</code>:
  305
+                <ul>{% for t in loader.templates %}<li><code>{{ t.name }}</code> (File {% if t.exists %}exists{% else %}does not exist{% endif %})</li>{% endfor %}</ul>
  306
+            </li>
  307
+        {% endfor %}
  308
+        </ul>
  309
+    {% else %}
  310
+        <p>Django couldn't find any templates because your <code>TEMPLATE_LOADERS</code> setting is empty!</p>
  311
+    {% endif %}
  312
+</div>
  313
+{% endif %}
275 314
 {% if template_info %}
276 315
 <div id="template">
277 316
    <h2>Template error</h2>

0 notes on commit d922825

Please sign in to comment.
Something went wrong with that request. Please try again.