From 4e400dcb79bc468ee1ff899312a00ed8dc3e795c Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Mon, 20 Jun 2016 13:24:51 -0400 Subject: [PATCH] Refs #22384 -- Readded RegexURLResolver.reverse(). It was removed in 785cc71d5b3300e2702b0b2fc7316e58ca70b563 only because it was untested and unused in Django itself, however, some third-party apps use it. --- django/urls/resolvers.py | 3 +++ tests/urlpatterns_reverse/tests.py | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/django/urls/resolvers.py b/django/urls/resolvers.py index eba1536580..f650346b02 100644 --- a/django/urls/resolvers.py +++ b/django/urls/resolvers.py @@ -323,6 +323,9 @@ class RegexURLResolver(LocaleRegexProvider): callback = getattr(urls, 'handler%s' % view_type) return get_callable(callback), {} + def reverse(self, lookup_view, *args, **kwargs): + return self._reverse_with_prefix(lookup_view, '', *args, **kwargs) + def _reverse_with_prefix(self, lookup_view, _prefix, *args, **kwargs): if args and kwargs: raise ValueError("Don't mix *args and **kwargs in call to reverse()!") diff --git a/tests/urlpatterns_reverse/tests.py b/tests/urlpatterns_reverse/tests.py index b0cb167bd8..bce4237b03 100644 --- a/tests/urlpatterns_reverse/tests.py +++ b/tests/urlpatterns_reverse/tests.py @@ -364,6 +364,12 @@ class ResolverTests(unittest.TestCase): except TypeError: self.fail('Failed to coerce lazy object to text') + def test_resolver_reverse(self): + resolver = get_resolver('urlpatterns_reverse.named_urls') + self.assertEqual(resolver.reverse('named-url1'), '') + self.assertEqual(resolver.reverse('named-url2', 'arg'), 'extra/arg/') + self.assertEqual(resolver.reverse('named-url2', extra='arg'), 'extra/arg/') + def test_non_regex(self): """ Verifies that we raise a Resolver404 if what we are resolving doesn't