diff --git a/django/shortcuts/__init__.py b/django/shortcuts/__init__.py index 823c6bb5e7..119bd092b5 100644 --- a/django/shortcuts/__init__.py +++ b/django/shortcuts/__init__.py @@ -49,6 +49,9 @@ def redirect(to, *args, **kwargs): try: return redirect_class(urlresolvers.reverse(to, args=args, kwargs=kwargs)) except urlresolvers.NoReverseMatch: + # If this is a callable, re-raise. + if callable(to): + raise # If this doesn't "feel" like a URL, re-raise. if '/' not in to and '.' not in to: raise diff --git a/tests/regressiontests/urlpatterns_reverse/tests.py b/tests/regressiontests/urlpatterns_reverse/tests.py index 1dc85b0424..d5f3ec9546 100644 --- a/tests/regressiontests/urlpatterns_reverse/tests.py +++ b/tests/regressiontests/urlpatterns_reverse/tests.py @@ -165,6 +165,12 @@ class ReverseShortcutTests(TestCase): res = redirect('http://example.com/') self.assertEqual(res['Location'], 'http://example.com/') + def test_redirect_view_object(self): + from views import absolute_kwargs_view + res = redirect(absolute_kwargs_view) + self.assertEqual(res['Location'], '/absolute_arg_view/') + self.assertRaises(NoReverseMatch, redirect, absolute_kwargs_view, wrong_argument=None) + class NamespaceTests(TestCase): urls = 'regressiontests.urlpatterns_reverse.namespace_urls'