diff --git a/django/core/urlresolvers.py b/django/core/urlresolvers.py index 1422bf7089d..14fc2822815 100644 --- a/django/core/urlresolvers.py +++ b/django/core/urlresolvers.py @@ -311,6 +311,7 @@ class RegexURLResolver(LocaleRegexProvider): return self._app_dict[language_code] def resolve(self, path): + path = force_text(path) # path may be a reverse_lazy object tried = [] match = self.regex.search(path) if match: diff --git a/tests/urlpatterns_reverse/tests.py b/tests/urlpatterns_reverse/tests.py index aef0fa05147..fd0f2cd6c28 100644 --- a/tests/urlpatterns_reverse/tests.py +++ b/tests/urlpatterns_reverse/tests.py @@ -8,7 +8,7 @@ import unittest from django.conf import settings from django.contrib.auth.models import User from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist -from django.core.urlresolvers import (reverse, resolve, get_callable, +from django.core.urlresolvers import (reverse, reverse_lazy, resolve, get_callable, get_resolver, NoReverseMatch, Resolver404, ResolverMatch, RegexURLResolver, RegexURLPattern) from django.http import HttpRequest, HttpResponseRedirect, HttpResponsePermanentRedirect @@ -219,6 +219,19 @@ class ResolverTests(unittest.TestCase): sub_resolver = resolver.namespace_dict['test-ns1'][1] self.assertIn('', repr(sub_resolver)) + def test_reverse_lazy_object_coercion_by_resolve(self): + """ + Verifies lazy object returned by reverse_lazy is coerced to + text by resolve(). Previous to #21043, this would raise a TypeError. + """ + urls = 'urlpatterns_reverse.named_urls' + proxy_url = reverse_lazy('named-url1', urlconf=urls) + resolver = get_resolver(urls) + try: + match = resolver.resolve(proxy_url) + except TypeError: + self.fail('Failed to coerce lazy object to text') + def test_non_regex(self): """ Verifies that we raise a Resolver404 if what we are resolving doesn't