Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed #10326: handler500 and handler404 may now be callables. Thanks,…

… dcwatson, adurdin.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@11854 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 1c5fe467bd838c8521ca7c7427d8196f08a24f80 1 parent 125403c
Jacob Kaplan-Moss jacobian authored
3  django/core/urlresolvers.py
View
@@ -257,9 +257,8 @@ def _get_url_patterns(self):
def _resolve_special(self, view_type):
callback = getattr(self.urlconf_module, 'handler%s' % view_type)
- mod_name, func_name = get_mod_func(callback)
try:
- return getattr(import_module(mod_name), func_name), {}
+ return get_callable(callback), {}
except (ImportError, AttributeError), e:
raise ViewDoesNotExist, "Tried %s. Error was: %s" % (callback, str(e))
16 docs/topics/http/urls.txt
View
@@ -253,24 +253,30 @@ handler404
.. data:: handler404
-A string representing the full Python import path to the view that should be
-called if none of the URL patterns match.
+A callable, or a string representing the full Python import path to the view
+that should be called if none of the URL patterns match.
By default, this is ``'django.views.defaults.page_not_found'``. That default
value should suffice.
+.. versionchanged:: 1.2
+ Previous versions of Django only accepted strings representing import paths.
+
handler500
----------
.. data:: handler500
-A string representing the full Python import path to the view that should be
-called in case of server errors. Server errors happen when you have runtime
-errors in view code.
+A callable, or a string representing the full Python import path to the view
+that should be called in case of server errors. Server errors happen when you
+have runtime errors in view code.
By default, this is ``'django.views.defaults.server_error'``. That default
value should suffice.
+.. versionchanged:: 1.2
+ Previous versions of Django only accepted strings representing import paths.
+
include
-------
23 tests/regressiontests/urlpatterns_reverse/tests.py
View
@@ -244,7 +244,6 @@ def test_app_lookup_object_without_default(self):
self.assertEquals('/other1/inner/37/42/', reverse('nodefault:urlobject-view', args=[37,42], current_app='other-ns1'))
self.assertEquals('/other1/inner/42/37/', reverse('nodefault:urlobject-view', kwargs={'arg1':42, 'arg2':37}, current_app='other-ns1'))
-
class RequestURLconfTests(TestCase):
def setUp(self):
self.root_urlconf = settings.ROOT_URLCONF
@@ -276,3 +275,25 @@ def test_urlconf_overridden(self):
response = self.client.get('/second_test/')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.content, 'outer:,inner:/second_test/')
+
+class ErrorHandlerResolutionTests(TestCase):
+ """Tests for handler404 and handler500"""
+
+ def setUp(self):
+ from django.core.urlresolvers import RegexURLResolver
+ urlconf = 'regressiontests.urlpatterns_reverse.urls_error_handlers'
+ urlconf_callables = 'regressiontests.urlpatterns_reverse.urls_error_handlers_callables'
+ self.resolver = RegexURLResolver(r'^$', urlconf)
+ self.callable_resolver = RegexURLResolver(r'^$', urlconf_callables)
+
+ def test_named_handlers(self):
+ from views import empty_view
+ handler = (empty_view, {})
+ self.assertEqual(self.resolver.resolve404(), handler)
+ self.assertEqual(self.resolver.resolve500(), handler)
+
+ def test_callable_handers(self):
+ from views import empty_view
+ handler = (empty_view, {})
+ self.assertEqual(self.callable_resolver.resolve404(), handler)
+ self.assertEqual(self.callable_resolver.resolve500(), handler)
8 tests/regressiontests/urlpatterns_reverse/urls_error_handlers.py
View
@@ -0,0 +1,8 @@
+# Used by the ErrorHandlerResolutionTests test case.
+
+from django.conf.urls.defaults import patterns
+
+urlpatterns = patterns('')
+
+handler404 = 'regressiontests.urlpatterns_reverse.views.empty_view'
+handler500 = 'regressiontests.urlpatterns_reverse.views.empty_view'
9 tests/regressiontests/urlpatterns_reverse/urls_error_handlers_callables.py
View
@@ -0,0 +1,9 @@
+# Used by the ErrorHandlerResolutionTests test case.
+
+from django.conf.urls.defaults import patterns
+from views import empty_view
+
+urlpatterns = patterns('')
+
+handler404 = empty_view
+handler500 = empty_view
Please sign in to comment.
Something went wrong with that request. Please try again.