From 859fc020a7c5ce30784d6388858ffbc613ef6612 Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Sat, 14 Aug 2010 14:27:35 +0000 Subject: [PATCH] =?UTF-8?q?Fixed=20#5350=20--=20Added=20fallback=20to=20de?= =?UTF-8?q?fault=20404/500=20handlers=20when=20they're=20not=20explicitly?= =?UTF-8?q?=20specified=20(or=20imported)=20in=20a=20urls.py=20file.=20Tha?= =?UTF-8?q?nks=20to=20Thomas=20G=C3=BCttler=20for=20the=20report=20and=20i?= =?UTF-8?q?nitial=20patch.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://code.djangoproject.com/svn/django/trunk@13590 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/core/urlresolvers.py | 7 ++++++- .../regressiontests/urlpatterns_reverse/tests.py | 16 ++++++++++++++++ .../regressiontests/urlpatterns_reverse/views.py | 13 +++++++++---- 3 files changed, 31 insertions(+), 5 deletions(-) diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py index 6881bbda9f..2d999d0de7 100644 --- a/django/core/urlresolvers.py +++ b/django/core/urlresolvers.py @@ -284,7 +284,12 @@ class RegexURLResolver(object): url_patterns = property(_get_url_patterns) def _resolve_special(self, view_type): - callback = getattr(self.urlconf_module, 'handler%s' % view_type) + callback = getattr(self.urlconf_module, 'handler%s' % view_type, None) + if not callback: + # No handler specified in file; use default + # Lazy import, since urls.defaults imports this file + from django.conf.urls import defaults + callback = getattr(defaults, 'handler%s' % view_type) try: return get_callable(callback), {} except (ImportError, AttributeError), e: diff --git a/tests/regressiontests/urlpatterns_reverse/tests.py b/tests/regressiontests/urlpatterns_reverse/tests.py index dcd942649c..008b5700dd 100644 --- a/tests/regressiontests/urlpatterns_reverse/tests.py +++ b/tests/regressiontests/urlpatterns_reverse/tests.py @@ -356,6 +356,22 @@ class ErrorHandlerResolutionTests(TestCase): self.assertEqual(self.callable_resolver.resolve404(), handler) self.assertEqual(self.callable_resolver.resolve500(), handler) +class DefaultErrorHandlerTests(TestCase): + urls = 'regressiontests.urlpatterns_reverse.urls_without_full_import' + + def test_default_handler(self): + "If the urls.py doesn't specify handlers, the defaults are used" + try: + response = self.client.get('/test/') + self.assertEquals(response.status_code, 404) + except AttributeError: + self.fail("Shouldn't get an AttributeError due to undefined 404 handler") + + try: + self.assertRaises(ValueError, self.client.get, '/bad_view/') + except AttributeError: + self.fail("Shouldn't get an AttributeError due to undefined 500 handler") + class NoRootUrlConfTests(TestCase): """Tests for handler404 and handler500 if urlconf is None""" urls = None diff --git a/tests/regressiontests/urlpatterns_reverse/views.py b/tests/regressiontests/urlpatterns_reverse/views.py index 27dca3d0e2..fdd742382c 100644 --- a/tests/regressiontests/urlpatterns_reverse/views.py +++ b/tests/regressiontests/urlpatterns_reverse/views.py @@ -1,14 +1,19 @@ +from django.http import HttpResponse + def empty_view(request, *args, **kwargs): - pass + return HttpResponse('') def kwargs_view(request, arg1=1, arg2=2): - pass + return HttpResponse('') def absolute_kwargs_view(request, arg1=1, arg2=2): - pass + return HttpResponse('') class ViewClass(object): def __call__(self, request, *args, **kwargs): - pass + return HttpResponse('') view_class_instance = ViewClass() + +def bad_view(request, *args, **kwargs): + raise ValueError("I don't think I'm getting good value for this view")