Skip to content
Closed
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
Diff view
7 changes: 5 additions & 2 deletions django/urls/resolvers.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,6 @@ def _route_to_regex(route, is_endpoint=False):
For example, 'foo/<int:pk>' returns '^foo\\/(?P<pk>[0-9]+)'
and {'pk': <django.urls.converters.IntConverter>}.
"""
if not set(route).isdisjoint(string.whitespace):
raise ImproperlyConfigured("URL route '%s' cannot contain whitespace." % route)
original_route = route
parts = ['^']
converters = {}
Expand All @@ -218,6 +216,11 @@ def _route_to_regex(route, is_endpoint=False):
if not match:
parts.append(re.escape(route))
break
elif not set(match.group()).isdisjoint(string.whitespace):
raise ImproperlyConfigured(
"URL route '%s' cannot contain whitespace in angle brackets "
"<…>." % original_route
)
parts.append(re.escape(route[:match.start()]))
route = route[match.end():]
parameter = match['parameter']
Expand Down
18 changes: 14 additions & 4 deletions tests/urlpatterns/tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import string
import uuid

from django.conf.urls import url as conf_url
Expand Down Expand Up @@ -142,10 +143,19 @@ def test_invalid_converter(self):
with self.assertRaisesMessage(ImproperlyConfigured, msg):
path('foo/<nonexistent:var>/', empty_view)

def test_space_in_route(self):
msg = "URL route 'space/<int: num>' cannot contain whitespace."
with self.assertRaisesMessage(ImproperlyConfigured, msg):
path('space/<int: num>', empty_view)
def test_whitespace_in_route(self):
msg = (
"URL route 'space/<int:num>/extra/<str:%stest>' cannot contain "
"whitespace in angle brackets <…>"
)
for whitespace in string.whitespace:
with self.subTest(repr(whitespace)):
with self.assertRaisesMessage(ImproperlyConfigured, msg % whitespace):
path('space/<int:num>/extra/<str:%stest>' % whitespace, empty_view)
# Whitespaces are valid in paths.
p = path('space%s/<int:num>/' % string.whitespace, empty_view)
match = p.resolve('space%s/1/' % string.whitespace)
self.assertEqual(match.kwargs, {'num': 1})


@override_settings(ROOT_URLCONF='urlpatterns.converter_urls')
Expand Down