mirror of
https://github.com/django/django.git
synced 2026-02-09 02:49:25 +08:00
Fixed #36796 -- Handled lazy routes correctly in RoutePattern.match().
Coerce lazy route values to `str` at match time to support prefix and
endpoint matching when using `gettext_lazy()` route paths.
Regression in f920937c8a.
Thanks to Andrea Angelini for the report, and to Jake Howard and Jacob
Walls for reviews.
Co-authored-by: Natalia <124304+nessita@users.noreply.github.com>
This commit is contained in:
@@ -341,8 +341,8 @@ class RoutePattern(CheckURLMixin):
|
||||
if self._route == path:
|
||||
return "", (), {}
|
||||
# If this isn't an endpoint, the path should start with the route.
|
||||
elif path.startswith(self._route):
|
||||
return path.removeprefix(self._route), (), {}
|
||||
elif path.startswith(route := str(self._route)):
|
||||
return path.removeprefix(route), (), {}
|
||||
return None
|
||||
|
||||
def check(self):
|
||||
|
||||
@@ -31,3 +31,7 @@ Bugfixes
|
||||
* Fixed a crash in Django 6.0 caused by infinite recursion when calling
|
||||
``repr()`` on an unevaluated ``django.utils.csp.LazyNonce`` instance
|
||||
(:ticket:`36810`).
|
||||
|
||||
* Fixed a regression in Django 6.0 where :func:`~django.urls.path` routes
|
||||
defined using :func:`~django.utils.translation.gettext_lazy` failed to
|
||||
resolve correctly (:ticket:`36796`).
|
||||
|
||||
9
tests/urlpatterns/lazy_path_urls.py
Normal file
9
tests/urlpatterns/lazy_path_urls.py
Normal file
@@ -0,0 +1,9 @@
|
||||
from django.urls import include, path
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path(_("included_urls/"), include("urlpatterns.included_urls")),
|
||||
path(_("lazy/<slug:slug>/"), views.empty_view, name="lazy"),
|
||||
]
|
||||
@@ -3,6 +3,8 @@ from django.test.utils import override_settings
|
||||
from django.urls.resolvers import RegexPattern, RoutePattern, get_resolver
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from . import views
|
||||
|
||||
|
||||
class RegexPatternTests(SimpleTestCase):
|
||||
def test_str(self):
|
||||
@@ -19,6 +21,21 @@ class RoutePatternTests(SimpleTestCase):
|
||||
self.assertEqual(len(RoutePattern("translated/<int:foo>").converters), 1)
|
||||
self.assertEqual(len(RoutePattern(_("translated/<int:foo>")).converters), 1)
|
||||
|
||||
def test_match_lazy_route_without_converters(self):
|
||||
pattern = RoutePattern(_("test/"))
|
||||
result = pattern.match("test/child/")
|
||||
self.assertEqual(result, ("child/", (), {}))
|
||||
|
||||
def test_match_lazy_route_endpoint(self):
|
||||
pattern = RoutePattern(_("test/"), is_endpoint=True)
|
||||
result = pattern.match("test/")
|
||||
self.assertEqual(result, ("", (), {}))
|
||||
|
||||
def test_match_lazy_route_with_converters(self):
|
||||
pattern = RoutePattern(_("test/<int:pk>/"))
|
||||
result = pattern.match("test/123/child/")
|
||||
self.assertEqual(result, ("child/", (), {"pk": 123}))
|
||||
|
||||
|
||||
class ResolverCacheTests(SimpleTestCase):
|
||||
@override_settings(ROOT_URLCONF="urlpatterns.path_urls")
|
||||
@@ -27,3 +44,17 @@ class ResolverCacheTests(SimpleTestCase):
|
||||
# settings.ROOT_URLCONF is the same cached object.
|
||||
self.assertIs(get_resolver(), get_resolver("urlpatterns.path_urls"))
|
||||
self.assertIsNot(get_resolver(), get_resolver("urlpatterns.path_dynamic_urls"))
|
||||
|
||||
|
||||
class ResolverLazyIncludeTests(SimpleTestCase):
|
||||
|
||||
def test_lazy_route_resolves(self):
|
||||
resolver = get_resolver("urlpatterns.lazy_path_urls")
|
||||
for url_path, name in [
|
||||
("/lazy/test-me/", "lazy"),
|
||||
("/included_urls/extra/test/", "inner-extra"),
|
||||
]:
|
||||
with self.subTest(name=name):
|
||||
match = resolver.resolve(url_path)
|
||||
self.assertEqual(match.func, views.empty_view)
|
||||
self.assertEqual(match.url_name, name)
|
||||
|
||||
Reference in New Issue
Block a user