From eb5d7bc2f465b055f4eb54a3d238502bdddb6d7e Mon Sep 17 00:00:00 2001 From: Alasdair Nicol Date: Sat, 2 Apr 2016 12:49:12 +0200 Subject: Fixed #26440 -- Added a warning for non-url()s in urlpatterns. Thanks Burhan Khalid for the initial patch and knbk/timgraham for review. --- django/core/checks/urls.py | 35 +++++++++++++++++++++++++++++++++-- 1 file changed, 33 insertions(+), 2 deletions(-) (limited to 'django') diff --git a/django/core/checks/urls.py b/django/core/checks/urls.py index 176cd16190..1b4db4b921 100644 --- a/django/core/checks/urls.py +++ b/django/core/checks/urls.py @@ -1,8 +1,10 @@ from __future__ import unicode_literals +import six + from django.conf import settings -from . import Tags, Warning, register +from . import Error, Tags, Warning, register @register(Tags.urls) @@ -27,12 +29,41 @@ def check_resolver(resolver): warnings.extend(check_resolver(pattern)) elif isinstance(pattern, RegexURLPattern): warnings.extend(check_pattern_name(pattern)) + else: + # This is not a url() instance + warnings.extend(get_warning_for_invalid_pattern(pattern)) - warnings.extend(check_pattern_startswith_slash(pattern)) + if not warnings: + warnings.extend(check_pattern_startswith_slash(pattern)) return warnings +def get_warning_for_invalid_pattern(pattern): + """ + Return a list containing a warning that the pattern is invalid. + + describe_pattern() cannot be used here, because we cannot rely on the + urlpattern having regex or name attributes. + """ + if isinstance(pattern, six.string_types): + hint = ( + "Try removing the string '{}'. The list of urlpatterns should not " + "have a prefix string as the first element.".format(pattern) + ) + elif isinstance(pattern, tuple): + hint = "Try using url() instead of a tuple." + else: + hint = None + + return [Error( + "Your URL pattern {!r} is invalid. Ensure that urlpatterns is a list " + "of url() instances.".format(pattern), + hint=hint, + id="urls.E004", + )] + + def describe_pattern(pattern): """ Format the URL pattern for display in warning messages. -- cgit v1.3