diff --git a/django/core/checks/urls.py b/django/core/checks/urls.py index b045fa0aae..8356fc2f00 100644 --- a/django/core/checks/urls.py +++ b/django/core/checks/urls.py @@ -97,8 +97,11 @@ def check_pattern_startswith_slash(pattern): regex_pattern = pattern.regex.pattern if regex_pattern.startswith('/') or regex_pattern.startswith('^/'): warning = Warning( - "Your URL pattern {} has a regex beginning with a '/'. " - "Remove this slash as it is unnecessary.".format(describe_pattern(pattern)), + "Your URL pattern {} has a regex beginning with a '/'. Remove this " + "slash as it is unnecessary. If this pattern is targeted in an " + "include(), ensure the include() pattern has a trailing '/'.".format( + describe_pattern(pattern) + ), id="urls.W002", ) return [warning] diff --git a/docs/ref/checks.txt b/docs/ref/checks.txt index 78d3783b59..a38cd1ec3d 100644 --- a/docs/ref/checks.txt +++ b/docs/ref/checks.txt @@ -657,6 +657,8 @@ The following checks are performed on your URL configuration: including URLs. * **urls.W002**: Your URL pattern ```` has a ``regex`` beginning with a ``/``. Remove this slash as it is unnecessary. + If this pattern is targeted in an :func:`~django.conf.urls.include`, ensure + the :func:`~django.conf.urls.include` pattern has a trailing ``/``. * **urls.W003**: Your URL pattern ```` has a ``name`` including a ``:``. Remove the colon, to avoid ambiguous namespace references. diff --git a/tests/check_framework/test_urls.py b/tests/check_framework/test_urls.py index 786d040493..926d1947dd 100644 --- a/tests/check_framework/test_urls.py +++ b/tests/check_framework/test_urls.py @@ -46,7 +46,13 @@ class CheckUrlsTest(SimpleTestCase): self.assertEqual(len(result), 1) warning = result[0] self.assertEqual(warning.id, 'urls.W002') - expected_msg = "Your URL pattern '/starting-with-slash/$' has a regex beginning with a '/'" + expected_msg = ( + "Your URL pattern '/starting-with-slash/$' has a regex beginning " + "with a '/'. Remove this slash as it is unnecessary. If this " + "pattern is targeted in an include(), ensure the include() pattern " + "has a trailing '/'." + ) + self.assertIn(expected_msg, warning.msg) @override_settings(ROOT_URLCONF='check_framework.urls.name_with_colon')