Refs #22384 -- Readded RegexURLResolver.reverse().

It was removed in 785cc71d5b only because
it was untested and unused in Django itself, however, some third-party
apps use it.
This commit is contained in:
Tim Graham 2016-06-20 13:24:51 -04:00
parent 2eb7cb2fff
commit 4e400dcb79
2 changed files with 9 additions and 0 deletions

View File

@ -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()!")

View File

@ -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