Django

Code

Changeset 1299

Show
Ignore:
Timestamp:
11/20/05 09:38:14 (3 years ago)
Author:
rjwittams
Message:

Integrated template errors into new error pages.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/branches/new-admin/django/core/template/defaultfilters.py

    r1288 r1299  
    201201    the argument. 
    202202    """ 
    203     print "ds:", type(value) 
    204203    decorated = [(resolve_variable('var.' + arg, {'var' : item}), item) for item in value] 
    205204    decorated.sort() 
  • django/branches/new-admin/django/core/template/__init__.py

    r1244 r1299  
    826826                e.source = node.source 
    827827            raise 
    828         except Exception, e: 
    829             from traceback import extract_tb, format_list, format_exception_only 
     828        except Exception: 
    830829            from sys import exc_info 
    831             t,v,tb = exc_info() 
    832             frames = extract_tb(tb) 
    833             frames.pop(0) 
    834             wrapped = TemplateSyntaxError( 'Caught exception:\n  %s' 
    835                                             %  "".join(format_exception_only(t,v)).replace('\n','')) 
     830            wrapped = TemplateSyntaxError( 'Caught exception whilst rendering' ) 
    836831            wrapped.source = node.source 
    837             wrapped.traceback = "".join(format_list(frames)
     832            wrapped.exc_info = exc_info(
    838833            raise wrapped 
    839834        return result 
  • django/branches/new-admin/django/views/debug.py

    r1288 r1299  
    77from django.core.template import Template, Context 
    88from django.utils.httpwrappers import HttpResponseServerError, HttpResponseNotFound 
     9from itertools import count, izip 
     10from django.utils.html import escape 
    911 
    1012HIDDEN_SETTINGS = re.compile('SECRET|PASSWORD') 
     13 
     14def linebreak_iter(template_source): 
     15        import re 
     16        newline_re = re.compile("^", re.M) 
     17        for match in newline_re.finditer(template_source): 
     18            yield match.start() 
     19        yield len(template_source) + 1 
     20 
     21def get_template_exception_info(exc_type,exc_value,tb): 
     22    origin, (start, end) = exc_value.source 
     23    template_source = origin.reload() 
     24    context_lines = 10 
     25    line = 0 
     26    upto = 0 
     27    source_lines = [] 
     28    linebreaks = izip(count(0), linebreak_iter(template_source)) 
     29    linebreaks.next() # skip the nothing before initial line start 
     30    for num, next in linebreaks: 
     31        if start >= upto and end <= next : 
     32            line = num 
     33            before = escape(template_source[upto:start]) 
     34            during = escape(template_source[start:end]) 
     35            after = escape(template_source[end:next - 1]) 
     36         
     37        source_lines.append( (num, escape(template_source[upto:next - 1])) ) 
     38        upto = next 
     39         
     40    total = len(source_lines) 
     41     
     42    top = max(0, line - context_lines) 
     43    bottom = min(total, line + 1 + context_lines) 
     44     
     45    template_info =  { 
     46                   'message'      : exc_value.args[0],  
     47                   'source_lines' : source_lines[top:bottom], 
     48                   'before' : before,  
     49                   'during': during, 
     50                   'after': after, 
     51                   'top': top , 
     52                   'bottom': bottom , 
     53                   'total' : total, 
     54                   'line'  : line, 
     55                   'name' : origin.name, 
     56                } 
     57    exc_info = hasattr(exc_value, 'exc_info') and exc_value.exc_info or (exc_type,exc_value,tb) 
     58    return exc_info + (template_info,) 
    1159 
    1260def technical_500_response(request, exc_type, exc_value, tb): 
     
    1563    the values returned from sys.exc_info() and friends. 
    1664    """ 
     65    template_info = None 
     66    if settings.TEMPLATE_DEBUG and hasattr(exc_value, 'source'): 
     67        exc_type, exc_value, tb, template_info = get_template_exception_info(exc_type,exc_value,tb) 
    1768    frames = [] 
    1869    while tb is not None: 
     
    54105        'request_protocol' : os.environ.get("HTTPS") == "on" and "https" or "http", 
    55106        'settings' : settings_dict, 
    56  
     107        'template_info': template_info, 
    57108    }) 
    58109    return HttpResponseServerError(t.render(c)) 
     
    145196    #requestinfo h2, #requestinfo h3 { position:relative; margin-left:-100px; } 
    146197    #requestinfo h3 { margin-bottom:-1em; } 
     198    table.source td{ font-family: monospace; white-space: pre;} 
     199    span.specific{background:#ffcab7;} 
     200    .error { background:#ffc; } 
    147201  </style> 
    148202  <script type="text/javascript"> 
     
    222276  </table> 
    223277</div> 
    224  
     278{%if template_info %} 
     279<div id="template"> 
     280   <h2>Template</h2> 
     281   In template {{template_info.name}}, error at line {{template_info.line}} 
     282   <div>{{template_info.message|escape}}</div> 
     283   <table class="source{%if template_info.top%} cut-top{%endif%}{%ifnotequal template_info.bottom template_info.total%} cut-bottom{%endifnotequal%}"> 
     284   {% for source_line in template_info.source_lines %} 
     285   {%ifequal source_line.0 template_info.line %} 
     286       <tr class="error"><td>{{source_line.0}}</td> 
     287       <td> {{template_info.before}}<span class="specific">{{template_info.during}}</span>{{template_info.after}}</td></tr> 
     288   {%else%} 
     289      <tr><td>{{source_line.0}}</td> 
     290      <td> {{source_line.1}}</td></tr> 
     291   {%endifequal%} 
     292   {%endfor%} 
     293   </table> 
     294</div> 
     295{%endif%} 
    225296<div id="traceback"> 
    226297  <h2>Traceback <span>(innermost last)</span></h2>