8000 Fixed #35518 -- Used simpler string operations for converter-less routes by RealOrangeOne · Pull Request #18270 · django/django · GitHub
[go: up one dir, main page]

Skip to content

Fixed #35518 -- Used simpler string operations for converter-less routes #18270

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
8000
Diff view
30 changes: 19 additions & 11 deletions django/urls/resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,17 +322,25 @@ def __init__(self, route, name=None, is_endpoint=False):
self.name = name

def match(self, path):
match = self.regex.search(path)
if match:
# RoutePattern doesn't allow non-named groups so args are ignored.
kwargs = match.groupdict()
for key, value in kwargs.items():
converter = self.converters[key]
try:
kwargs[key] = converter.to_python(value)
except ValueError:
return None
return path[match.end() :], (), kwargs
# Only use regex overhead if there are converters.
if self.converters:
if match := self.regex.search(path):
# RoutePattern doesn't allow non-named groups so args are ignored.
kwargs = match.groupdict()
for key, value in kwargs.items():
converter = self.converters[key]
try:
kwargs[key] = converter.to_python(value)
except ValueError:
return None
return path[match.end() :], (), kwargs
# If this is an endpoint, the path should be exactly the same as the route.
elif self._is_endpoint:
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), (), {}
return None

def check(self):
Expand Down
6 changes: 6 additions & 0 deletions tests/urlpatterns/test_resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ class RoutePatternTests(SimpleTestCase):
def test_str(self):
self.assertEqual(str(RoutePattern(_("translated/"))), "translated/")

def test_has_converters(self):
self.assertEqual(len(RoutePattern("translated/").converters), 0)
self.assertEqual(len(RoutePattern(_("translated/")).converters), 0)
self.assertEqual(len(RoutePattern("translated/<int:foo>").converters), 1)
self.assertEqual(len(RoutePattern(_("translated/<int:foo>")).converters), 1)


class ResolverCacheTests(SimpleTestCase):
@override_settings(ROOT_URLCONF="urlpatterns.path_urls")
Expand Down
Loading
0