From 19bfdadc430fa8d366de1b8ca821cc4c64341b67 Mon Sep 17 00:00:00 2001 From: Ramiro Morales Date: Thu, 20 Jan 2011 22:33:58 +0000 Subject: [PATCH] Fixed #15122 -- Restored reporting of the template files tried in the texmplate loader post mortem section of the TemplateDoesNotExit 500 error debug page. Thanks rdrey for reporting this regression. git-svn-id: http://code.djangoproject.com/svn/django/trunk@15252 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/views/debug.py | 11 ++++++----- tests/regressiontests/views/tests/debug.py | 4 ++++ tests/regressiontests/views/urls.py | 1 + tests/regressiontests/views/views.py | 10 +++++++++- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/django/views/debug.py b/django/views/debug.py index 15109840b1..f7888c3a4b 100644 --- a/django/views/debug.py +++ b/django/views/debug.py @@ -89,17 +89,18 @@ class ExceptionReporter: for loader in template_source_loaders: try: module = import_module(loader.__module__) - source_list_func = module.get_template_sources + if hasattr(loader, '__class__'): + source_list_func = loader.get_template_sources + loader_name = loader.__module__ + '.' + loader.__class__.__name__ + else: # NOTE: Remember to remove this branch when we deprecate old template loaders in 1.4 + source_list_func = module.get_template_sources + loader_name = loader.__module__ + '.' + loader.__name__ # NOTE: This assumes exc_value is the name of the template that # the loader attempted to load. template_list = [{'name': t, 'exists': os.path.exists(t)} \ for t in source_list_func(str(self.exc_value))] except (ImportError, AttributeError): template_list = [] - if hasattr(loader, '__class__'): - loader_name = loader.__module__ + '.' + loader.__class__.__name__ - else: - loader_name = loader.__module__ + '.' + loader.__name__ self.loader_debug_info.append({ 'loader': loader_name, 'templates': template_list, diff --git a/tests/regressiontests/views/tests/debug.py b/tests/regressiontests/views/tests/debug.py index 4ebe37f558..c71e8c502d 100644 --- a/tests/regressiontests/views/tests/debug.py +++ b/tests/regressiontests/views/tests/debug.py @@ -48,3 +48,7 @@ class DebugViewTests(TestCase): self.assertFalse(raising_loc.find('raise BrokenException') == -1, "Failed to find 'raise BrokenException' in last frame of traceback, instead found: %s" % raising_loc) + + def test_template_loader_postmortem(self): + response = self.client.get(reverse('raises_template_does_not_exist')) + self.assertContains(response, 'templates/i_dont_exist.html (File does not exist)', status_code=500) diff --git a/tests/regressiontests/views/urls.py b/tests/regressiontests/views/urls.py index a170efb1fc..b7ff3efbdc 100644 --- a/tests/regressiontests/views/urls.py +++ b/tests/regressiontests/views/urls.py @@ -143,6 +143,7 @@ urlpatterns += patterns('django.views.generic.simple', urlpatterns += patterns('regressiontests.views.views', url(r'view_exception/(?P\d+)/$', 'view_exception', name='view_exception'), url(r'template_exception/(?P\d+)/$', 'template_exception', name='template_exception'), + url(r'^raises_template_does_not_exist/$', 'raises_template_does_not_exist', name='raises_template_does_not_exist'), (r'^shortcuts/render_to_response/$', 'render_to_response_view'), (r'^shortcuts/render_to_response/request_context/$', 'render_to_response_view_with_request_context'), diff --git a/tests/regressiontests/views/views.py b/tests/regressiontests/views/views.py index 622e5f6442..bcff08e125 100644 --- a/tests/regressiontests/views/views.py +++ b/tests/regressiontests/views/views.py @@ -4,7 +4,7 @@ from django import forms from django.http import HttpResponse, HttpResponseRedirect from django.core.urlresolvers import get_resolver from django.shortcuts import render_to_response, render -from django.template import Context, RequestContext +from django.template import Context, RequestContext, TemplateDoesNotExist from django.views.debug import technical_500_response from django.views.generic.create_update import create_object @@ -120,3 +120,11 @@ def render_view_with_current_app_conflict(request): 'foo': 'FOO', 'bar': 'BAR', }, current_app="foobar_app", context_instance=RequestContext(request)) + +def raises_template_does_not_exist(request): + # We need to inspect the HTML generated by the fancy 500 debug view but + # the test client ignores it, so we send it explicitly. + try: + return render_to_response('i_dont_exist.html') + except TemplateDoesNotExist: + return technical_500_response(request, *sys.exc_info())