Ticket #10841: 10841.diff

File 10841.diff, 5.5 KB (added by Chris Beaven, 10 years ago)
  • django/views/debug.py

     
    3737    the values returned from sys.exc_info() and friends.
    3838    """
    3939    reporter = ExceptionReporter(request, exc_type, exc_value, tb)
     40    if not request.is_ajax():
     41        text = reporter.get_traceback_text()
     42        return HttpResponseServerError(text, mimetype='text/plain')
    4043    html = reporter.get_traceback_html()
    4144    return HttpResponseServerError(html, mimetype='text/html')
    4245
     
    5962            self.exc_value = Exception('Deprecated String Exception: %r' % self.exc_type)
    6063            self.exc_type = type(self.exc_value)
    6164
    62     def get_traceback_html(self):
    63         "Return HTML code for traceback."
    64 
     65    def get_traceback_data(self):
    6566        if issubclass(self.exc_type, TemplateDoesNotExist):
    6667            from django.template.loader import template_source_loaders
    6768            self.template_does_not_exist = True
     
    9394                unicode_str = self.exc_value.args[1]
    9495                unicode_hint = smart_unicode(unicode_str[max(start-5, 0):min(end+5, len(unicode_str))], 'ascii', errors='replace')
    9596        from django import get_version
    96         t = Template(TECHNICAL_500_TEMPLATE, name='Technical 500 template')
    97         c = Context({
     97        return {
    9898            'exception_type': self.exc_type.__name__,
    9999            'exception_value': smart_unicode(self.exc_value, errors='replace'),
    100100            'unicode_hint': unicode_hint,
     
    110110            'template_info': self.template_info,
    111111            'template_does_not_exist': self.template_does_not_exist,
    112112            'loader_debug_info': self.loader_debug_info,
    113         })
     113        }
     114
     115    def get_traceback_html(self):
     116        "Return HTML code version of traceback."
     117        t = Template(TECHNICAL_500_TEMPLATE, name='Technical 500 template')
     118        c = Context(self.get_traceback_data())
    114119        return t.render(c)
    115120
     121    def get_traceback_text(self):
     122        "Return plain text version of traceback."
     123        t = Template(TECHNICAL_500_TEXT_TEMPLATE,
     124                     name='Technical 500 template')
     125        c = Context(self.get_traceback_data(), autoescape=False)
     126        return t.render(c)
     127
    116128    def get_template_exception_info(self):
    117129        origin, (start, end) = self.exc_value.source
    118130        template_source = origin.reload()
     
    276288# always work even if the template loader is broken.
    277289#
    278290
     291TECHNICAL_500_TEXT_TEMPLATE = """{{ exception_type }} at {{ request.path_info }}
     292{{ exception_value }}
     293
     294Request Method: {{ request.META.REQUEST_METHOD }}
     295Request URL: {{ request_protocol }}://{{ request.META.HTTP_HOST }}{{ request.path_info }}
     296
     297Server time: {{server_time|date:"r"}}
     298Django Version: {{ django_version_info }}
     299Python Version: {{ sys_version_info }}
     300Python Path: {{ sys_path }}
     301Python Executable: {{ sys_executable }}
     302Installed Applications:
     303{{ settings.INSTALLED_APPS|pprint }}
     304Installed Middleware:
     305{{ settings.MIDDLEWARE_CLASSES|pprint }}
     306
     307{% if template_does_not_exist %}Template Loader Error:
     308{% if loader_debug_info %}Django tried loading these templates, in this order:
     309{% for loader in loader_debug_info %}Using loader {{ loader.loader }}:
     310{% for t in loader.templates %}{{ t.name }} (File {% if t.exists %}exists{% else %}does not exist{% endif %})
     311{% endfor %}{% endfor %}
     312{% else %}Django couldn't find any templates because your TEMPLATE_LOADERS setting is empty!
     313{% endif %}
     314{% endif %}{% if template_info %}
     315Template error:
     316In template {{ template_info.name }}, error at line {{ template_info.line }}
     317   {{ template_info.message }}{% for source_line in template_info.source_lines %}{% ifequal source_line.0 template_info.line %}
     318   {{ source_line.0 }} : {{ template_info.before }} {{ template_info.during }} {{ template_info.after }}
     319{% else %}
     320   {{ source_line.0 }} : {{ source_line.1 }}
     321{% endifequal %}{% endfor %}{% endif %}
     322Traceback:
     323{% for frame in frames %}File "{{ frame.filename }}" in {{ frame.function }}
     324{% if frame.context_line %}  {{ frame.lineno }}. {{ frame.context_line }}{% endif %}
     325{% endfor %}
     326Exception Type: {{ exception_type }} at {{ request.path_info }}
     327Exception Value: {{ exception_value }}
     328
     329Request information:
     330GET:{% for k, v in request.GET.items %}
     331{{ k }} = {{ v|stringformat:"r" }}{% empty %} No GET data{% endfor %}
     332
     333POST:{% for k, v in request.POST.items %}
     334{{ k }} = {{ v|stringformat:"r" }}{% empty %} No POST data{% endfor %}
     335
     336FILES:{% for k, v in request.FILES.items %}
     337{{ k }} = {{ v|stringformat:"r" }}{% empty %} No FILES data{% endfor %}
     338
     339COOKIES:{% for k, v in request.COOKIES.items %}
     340{{ k }} = {{ v|stringformat:"r" }}{% empty %} No cookie data{% endfor %}
     341
     342META:{% for k, v in request.META.items|dictsort:"0" %}
     343{{ k }} = {{ v|stringformat:"r" }}{% endfor %}
     344
     345Settings:
     346Using settings module {{ settings.SETTINGS_MODULE }}{% for k, v in settings.items|dictsort:"0" %}
     347{{ k }} = {{ v|stringformat:"r" }}{% endfor %}
     348
     349    You're seeing this error because you have DEBUG = True in your
     350    Django settings file. Change that to False, and Django will
     351    display a standard 500 page.
     352"""
     353
    279354TECHNICAL_500_TEMPLATE = """
    280355<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    281356<html lang="en">
     
    630705  {% else %}
    631706    <p>No FILES data</p>
    632707  {% endif %}
    633  
    634708
     709
    635710  <h3 id="cookie-info">COOKIES</h3>
    636711  {% if request.COOKIES %}
    637712    <table class="req">
Back to Top