diff options
| author | Alasdair Nicol <alasdair@thenicols.net> | 2016-04-02 12:49:12 +0200 |
|---|---|---|
| committer | Tim Graham <timograham@gmail.com> | 2016-04-30 20:09:31 -0400 |
| commit | eb5d7bc2f465b055f4eb54a3d238502bdddb6d7e (patch) | |
| tree | 597da9f4f1a56316ee5f8616d30638604c3f69a8 /django | |
| parent | 914c72be2abb1c6dd860cb9279beaa66409ae1b2 (diff) | |
Fixed #26440 -- Added a warning for non-url()s in urlpatterns.
Thanks Burhan Khalid for the initial patch and knbk/timgraham
for review.
Diffstat (limited to 'django')
| -rw-r--r-- | django/core/checks/urls.py | 35 |
1 files changed, 33 insertions, 2 deletions
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. |
