Django

Code

Changeset 1400

Show
Ignore:
Timestamp:
11/24/05 15:15:51 (2 years ago)
Author:
adrian
Message:

Fixed #892 -- TemplateDoesNotExist? errors now get a 'Template-loader postmortem' section on the pretty debug page. Thanks for the idea, David Ascher

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/django/views/debug.py

    r1385 r1400  
    11from django.conf import settings 
    2 from django.core.template import Template, Context 
     2from django.core.template import Template, Context, TemplateDoesNotExist 
    33from django.utils.html import escape 
    44from django.utils.httpwrappers import HttpResponseServerError, HttpResponseNotFound 
     
    4444        'during': during, 
    4545        'after': after, 
    46         'top': top
    47         'bottom': bottom
     46        'top': top
     47        'bottom': bottom
    4848        'total': total, 
    4949        'line': line, 
     
    5959    """ 
    6060    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            }) 
    6180    if settings.TEMPLATE_DEBUG and hasattr(exc_value, 'source'): 
    6281        exc_type, exc_value, tb, template_info = get_template_exception_info(exc_type, exc_value, tb) 
     
    101120        'settings': settings_dict, 
    102121        'template_info': template_info, 
     122        'template_does_not_exist': template_does_not_exist, 
     123        'loader_debug_info': loader_debug_info, 
    103124    }) 
    104125    return HttpResponseServerError(t.render(c), mimetype='text/html') 
     
    188209    #summary h2 { font-weight: normal; color: #666; } 
    189210    #explanation { background:#eee; } 
    190     #template { background:#f6f6f6; } 
     211    #template, #template-not-exist { background:#f6f6f6; } 
     212    #template-not-exist ul { margin: 0 0 0 20px; } 
    191213    #traceback { background:#eee; } 
    192214    #requestinfo { background:#f6f6f6; padding-left:120px; } 
     
    273295  </table> 
    274296</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 %} 
    275314{% if template_info %} 
    276315<div id="template">