Fixed #21043 -- Made resolve() handle reverse_lazy objects.

Thanks Keryn Knight for the report.
This commit is contained in:
Dan Johnson 2013-09-06 13:43:58 -05:00 committed by Tim Graham
parent c7c19ac408
commit df462cf760
2 changed files with 15 additions and 1 deletions

View File

@ -311,6 +311,7 @@ class RegexURLResolver(LocaleRegexProvider):
return self._app_dict[language_code] return self._app_dict[language_code]
def resolve(self, path): def resolve(self, path):
path = force_text(path) # path may be a reverse_lazy object
tried = [] tried = []
match = self.regex.search(path) match = self.regex.search(path)
if match: if match:

View File

@ -8,7 +8,7 @@ import unittest
from django.conf import settings from django.conf import settings
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.exceptions import ImproperlyConfigured, ViewDoesNotExist 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, get_resolver, NoReverseMatch, Resolver404, ResolverMatch, RegexURLResolver,
RegexURLPattern) RegexURLPattern)
from django.http import HttpRequest, HttpResponseRedirect, HttpResponsePermanentRedirect from django.http import HttpRequest, HttpResponseRedirect, HttpResponsePermanentRedirect
@ -219,6 +219,19 @@ class ResolverTests(unittest.TestCase):
sub_resolver = resolver.namespace_dict['test-ns1'][1] sub_resolver = resolver.namespace_dict['test-ns1'][1]
self.assertIn('<RegexURLPattern list>', repr(sub_resolver)) self.assertIn('<RegexURLPattern list>', 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): def test_non_regex(self):
""" """
Verifies that we raise a Resolver404 if what we are resolving doesn't Verifies that we raise a Resolver404 if what we are resolving doesn't