mirror of
https://github.com/django/django.git
synced 2025-06-12 15:09:12 +00:00
Fixed #35518 -- Optimized RoutePattern by using string operations for converter-less routes.
This commit is contained in:
parent
f66c79e93d
commit
f920937c8a
@ -322,17 +322,25 @@ class RoutePattern(CheckURLMixin):
|
|||||||
self.name = name
|
self.name = name
|
||||||
|
|
||||||
def match(self, path):
|
def match(self, path):
|
||||||
match = self.regex.search(path)
|
# Only use regex overhead if there are converters.
|
||||||
if match:
|
if self.converters:
|
||||||
# RoutePattern doesn't allow non-named groups so args are ignored.
|
if match := self.regex.search(path):
|
||||||
kwargs = match.groupdict()
|
# RoutePattern doesn't allow non-named groups so args are ignored.
|
||||||
for key, value in kwargs.items():
|
kwargs = match.groupdict()
|
||||||
converter = self.converters[key]
|
for key, value in kwargs.items():
|
||||||
try:
|
converter = self.converters[key]
|
||||||
kwargs[key] = converter.to_python(value)
|
try:
|
||||||
except ValueError:
|
kwargs[key] = converter.to_python(value)
|
||||||
return None
|
except ValueError:
|
||||||
return path[match.end() :], (), kwargs
|
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
|
return None
|
||||||
|
|
||||||
def check(self):
|
def check(self):
|
||||||
|
@ -13,6 +13,12 @@ class RoutePatternTests(SimpleTestCase):
|
|||||||
def test_str(self):
|
def test_str(self):
|
||||||
self.assertEqual(str(RoutePattern(_("translated/"))), "translated/")
|
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):
|
class ResolverCacheTests(SimpleTestCase):
|
||||||
@override_settings(ROOT_URLCONF="urlpatterns.path_urls")
|
@override_settings(ROOT_URLCONF="urlpatterns.path_urls")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user