Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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 authored
3  django/core/urlresolvers.py
@@ -257,9 +257,8 @@ def _get_url_patterns(self):
257 257
 
258 258
     def _resolve_special(self, view_type):
259 259
         callback = getattr(self.urlconf_module, 'handler%s' % view_type)
260  
-        mod_name, func_name = get_mod_func(callback)
261 260
         try:
262  
-            return getattr(import_module(mod_name), func_name), {}
  261
+            return get_callable(callback), {}
263 262
         except (ImportError, AttributeError), e:
264 263
             raise ViewDoesNotExist, "Tried %s. Error was: %s" % (callback, str(e))
265 264
 
16  docs/topics/http/urls.txt
@@ -253,24 +253,30 @@ handler404
253 253
 
254 254
 .. data:: handler404
255 255
 
256  
-A string representing the full Python import path to the view that should be
257  
-called if none of the URL patterns match.
  256
+A callable, or a string representing the full Python import path to the view
  257
+that should be called if none of the URL patterns match.
258 258
 
259 259
 By default, this is ``'django.views.defaults.page_not_found'``. That default
260 260
 value should suffice.
261 261
 
  262
+.. versionchanged:: 1.2
  263
+    Previous versions of Django only accepted strings representing import paths.
  264
+
262 265
 handler500
263 266
 ----------
264 267
 
265 268
 .. data:: handler500
266 269
 
267  
-A string representing the full Python import path to the view that should be
268  
-called in case of server errors. Server errors happen when you have runtime
269  
-errors in view code.
  270
+A callable, or a string representing the full Python import path to the view
  271
+that should be called in case of server errors. Server errors happen when you
  272
+have runtime errors in view code.
270 273
 
271 274
 By default, this is ``'django.views.defaults.server_error'``. That default
272 275
 value should suffice.
273 276
 
  277
+.. versionchanged:: 1.2
  278
+    Previous versions of Django only accepted strings representing import paths.
  279
+
274 280
 include
275 281
 -------
276 282
 
23  tests/regressiontests/urlpatterns_reverse/tests.py
@@ -244,7 +244,6 @@ def test_app_lookup_object_without_default(self):
244 244
         self.assertEquals('/other1/inner/37/42/', reverse('nodefault:urlobject-view', args=[37,42], current_app='other-ns1'))
245 245
         self.assertEquals('/other1/inner/42/37/', reverse('nodefault:urlobject-view', kwargs={'arg1':42, 'arg2':37}, current_app='other-ns1'))
246 246
 
247  
-
248 247
 class RequestURLconfTests(TestCase):
249 248
     def setUp(self):
250 249
         self.root_urlconf = settings.ROOT_URLCONF
@@ -276,3 +275,25 @@ def test_urlconf_overridden(self):
276 275
         response = self.client.get('/second_test/')
277 276
         self.assertEqual(response.status_code, 200)
278 277
         self.assertEqual(response.content, 'outer:,inner:/second_test/')
  278
+
  279
+class ErrorHandlerResolutionTests(TestCase):
  280
+    """Tests for handler404 and handler500"""
  281
+    
  282
+    def setUp(self):
  283
+        from django.core.urlresolvers import RegexURLResolver
  284
+        urlconf = 'regressiontests.urlpatterns_reverse.urls_error_handlers'
  285
+        urlconf_callables = 'regressiontests.urlpatterns_reverse.urls_error_handlers_callables'
  286
+        self.resolver = RegexURLResolver(r'^$', urlconf)
  287
+        self.callable_resolver = RegexURLResolver(r'^$', urlconf_callables)
  288
+    
  289
+    def test_named_handlers(self):
  290
+        from views import empty_view
  291
+        handler = (empty_view, {})
  292
+        self.assertEqual(self.resolver.resolve404(), handler)
  293
+        self.assertEqual(self.resolver.resolve500(), handler)
  294
+
  295
+    def test_callable_handers(self):
  296
+        from views import empty_view
  297
+        handler = (empty_view, {})
  298
+        self.assertEqual(self.callable_resolver.resolve404(), handler)
  299
+        self.assertEqual(self.callable_resolver.resolve500(), handler)
8  tests/regressiontests/urlpatterns_reverse/urls_error_handlers.py
... ...
@@ -0,0 +1,8 @@
  1
+# Used by the ErrorHandlerResolutionTests test case.
  2
+
  3
+from django.conf.urls.defaults import patterns
  4
+
  5
+urlpatterns = patterns('')
  6
+
  7
+handler404 = 'regressiontests.urlpatterns_reverse.views.empty_view'
  8
+handler500 = 'regressiontests.urlpatterns_reverse.views.empty_view'
9  tests/regressiontests/urlpatterns_reverse/urls_error_handlers_callables.py
... ...
@@ -0,0 +1,9 @@
  1
+# Used by the ErrorHandlerResolutionTests test case.
  2
+
  3
+from django.conf.urls.defaults import patterns
  4
+from views import empty_view
  5
+
  6
+urlpatterns = patterns('')
  7
+
  8
+handler404 = empty_view
  9
+handler500 = empty_view

0 notes on commit 1c5fe46

Please sign in to comment.
Something went wrong with that request. Please try again.