Ticket #10326: handlerXXX_callable.2.diff

File handlerXXX_callable.2.diff, 4.4 KB (added by Andy Durdin, 15 years ago)

Updated patch against r10682; updated version in docs to 1.2; added tests.

  • django/core/urlresolvers.py

     
    216216
    217217    def _resolve_special(self, view_type):
    218218        callback = getattr(self.urlconf_module, 'handler%s' % view_type)
    219         mod_name, func_name = get_mod_func(callback)
    220219        try:
    221             return getattr(import_module(mod_name), func_name), {}
     220            return get_callable(callback), {}
    222221        except (ImportError, AttributeError), e:
    223222            raise ViewDoesNotExist, "Tried %s. Error was: %s" % (callback, str(e))
    224223
  • tests/regressiontests/urlpatterns_reverse/tests.py

     
    142142        res = redirect('/foo/')
    143143        self.assertEqual(res['Location'], '/foo/')
    144144        res = redirect('http://example.com/')
    145         self.assertEqual(res['Location'], 'http://example.com/')
    146  No newline at end of file
     145        self.assertEqual(res['Location'], 'http://example.com/')
     146
     147
     148class HandlerResolutionTests(TestCase):
     149    def setUp(self):
     150        from django.core.urlresolvers import RegexURLResolver
     151        urlconf = 'regressiontests.urlpatterns_reverse.urls_handlers'
     152        urlconf_callables = 'regressiontests.urlpatterns_reverse.urls_handlers_callable'
     153        self.resolver = RegexURLResolver(r'^$', urlconf)
     154        self.callable_resolver = RegexURLResolver(r'^$', urlconf_callables)
     155   
     156    def testNamedHandlers(self):
     157        from views import empty_view
     158        handler = (empty_view, {})
     159        self.assertEqual(self.resolver.resolve404(), handler)
     160        self.assertEqual(self.resolver.resolve500(), handler)
     161
     162    def testCallableHandlers(self):
     163        from views import empty_view
     164        handler = (empty_view, {})
     165        self.assertEqual(self.callable_resolver.resolve404(), handler)
     166        self.assertEqual(self.callable_resolver.resolve500(), handler)
  • tests/regressiontests/urlpatterns_reverse/urls_handlers_callable.py

     
     1from django.conf.urls.defaults import patterns
     2from views import empty_view
     3
     4urlpatterns = patterns('')
     5
     6handler404 = empty_view
     7handler500 = empty_view
  • tests/regressiontests/urlpatterns_reverse/urls_handlers.py

     
     1from django.conf.urls.defaults import patterns
     2
     3urlpatterns = patterns('')
     4
     5handler404 = 'regressiontests.urlpatterns_reverse.views.empty_view'
     6handler500 = 'regressiontests.urlpatterns_reverse.views.empty_view'
  • docs/topics/http/urls.txt

     
    244244handler404
    245245----------
    246246
    247 A string representing the full Python import path to the view that should be
    248 called if none of the URL patterns match.
     247A callable, or a string representing the full Python import path to the view
     248that should be called if none of the URL patterns match.
    249249
    250250By default, this is ``'django.views.defaults.page_not_found'``. That default
    251251value should suffice.
    252252
     253.. versionchanged:: 1.2
     254    Previous versions of Django only accepted strings representing import paths.
     255
    253256handler500
    254257----------
    255258
    256 A string representing the full Python import path to the view that should be
    257 called in case of server errors. Server errors happen when you have runtime
    258 errors in view code.
     259A callable, or a string representing the full Python import path to the view
     260that should be called in case of server errors. Server errors happen when you
     261have runtime errors in view code.
    259262
    260263By default, this is ``'django.views.defaults.server_error'``. That default
    261264value should suffice.
    262265
     266.. versionchanged:: 1.2
     267    Previous versions of Django only accepted strings representing import paths.
     268
    263269include
    264270-------
    265271
Back to Top