Opened 4 years ago

Closed 4 years ago

Last modified 4 years ago

#26155 closed Bug (fixed)

Skip URL checks if settings.ROOTURL_CONF isn't defined

Reported by: Buddy Lindsey Owned by: Buddy Lindsey
Component: Core (System checks) Version: 1.9
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

If you try to create a migration with makemigrations it throws an error (below) because it is running the check_url_config system check. There currently isn't a way to disable this check, and the use case we have we don't need/use urls.

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/buddy/.venv/sk-client/lib/python3.4/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/home/buddy/.venv/sk-client/lib/python3.4/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/buddy/.venv/sk-client/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/buddy/.venv/sk-client/lib/python3.4/site-packages/django/core/management/base.py", line 398, in execute
    self.check()
  File "/home/buddy/.venv/sk-client/lib/python3.4/site-packages/django/core/management/base.py", line 426, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/buddy/.venv/sk-client/lib/python3.4/site-packages/django/core/checks/registry.py", line 75, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/buddy/.venv/sk-client/lib/python3.4/site-packages/django/core/checks/urls.py", line 9, in check_url_config
    resolver = get_resolver()
  File "/home/buddy/.venv/sk-client/lib/python3.4/functools.py", line 448, in wrapper
    result = user_function(*args, **kwds)
  File "/home/buddy/.venv/sk-client/lib/python3.4/site-packages/django/core/urlresolvers.py", line 151, in get_resolver
    urlconf = settings.ROOT_URLCONF
  File "/home/buddy/.venv/sk-client/lib/python3.4/site-packages/django/conf/__init__.py", line 56, in __getattr__
    return getattr(self._wrapped, name)
AttributeError: 'Settings' object has no attribute 'ROOT_URLCONF'

Here are the steps to reproduce this error:

./manage.py startproject demo
cd blog
vim blog/settings.py    # comment out ROOT_URLCONF
./manage.py startapp blog
vim blog/models.py    # add model below
./manage.py makemigrations
from django.db import models

class Post(models.Model):
    title = models.CharField(max_length=, blank=True)
    description = models.TextField(blank=True)

That should be enough to reproduce the error.

My thought is we need to add a way to disable the check_url_config check.

Change History (10)

comment:1 Changed 4 years ago by Tim Graham

Summary: Can not Disable `check_url_config` Url CheckSkip URL checks if settings.ROOTURL_CONF isn't defined
Triage Stage: UnreviewedAccepted

Would a if hasattr(settings, 'ROOT_URLCONF') check be sufficient?

comment:2 Changed 4 years ago by Buddy Lindsey

Owner: changed from nobody to Buddy Lindsey
Status: newassigned

comment:3 Changed 4 years ago by Buddy Lindsey

I don't think it would because when I set settings.ROOT_URLCONF to an empty string it tries to run through the url resolver code which erros because it can't find the app it is assigned to, or any urlpatterns as it tries to resolve them.

I tried to tweak on what is returned where the ROOT_URLCONF is accessed, and the few things I tried didn't help.

Here is where the code is trying to access the attribute and is throwing the exception: https://github.com/django/django/blob/1.9.1/django/core/urlresolvers.py#L147-L152

comment:4 Changed 4 years ago by Tim Graham

if getattr(settings, ROOT_URLCONF, None)?

comment:5 Changed 4 years ago by Buddy Lindsey

If you return None for the settings of ROOT_URLCONF you get the following excpetion:

Traceback (most recent call last):
  File "/home/buddy/.venv/sk-client/lib/python3.4/site-packages/django/core/urlresolvers.py", line 419, in url_patterns
    iter(patterns)
TypeError: 'NoneType' object is not iterable

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/buddy/.venv/sk-client/lib/python3.4/site-packages/django/core/management/__init__.py", line 353, in execute_from_command_line
    utility.execute()
  File "/home/buddy/.venv/sk-client/lib/python3.4/site-packages/django/core/management/__init__.py", line 345, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/buddy/.venv/sk-client/lib/python3.4/site-packages/django/core/management/base.py", line 348, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/buddy/.venv/sk-client/lib/python3.4/site-packages/django/core/management/base.py", line 398, in execute
    self.check()
  File "/home/buddy/.venv/sk-client/lib/python3.4/site-packages/django/core/management/base.py", line 426, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/buddy/.venv/sk-client/lib/python3.4/site-packages/django/core/checks/registry.py", line 75, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/buddy/.venv/sk-client/lib/python3.4/site-packages/django/core/checks/urls.py", line 10, in check_url_config
    return check_resolver(resolver)
  File "/home/buddy/.venv/sk-client/lib/python3.4/site-packages/django/core/checks/urls.py", line 19, in check_resolver
    for pattern in resolver.url_patterns:
  File "/home/buddy/.venv/sk-client/lib/python3.4/site-packages/django/utils/functional.py", line 33, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/buddy/.venv/sk-client/lib/python3.4/site-packages/django/core/urlresolvers.py", line 426, in url_patterns
    raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf 'None' does not appear to have any patterns in it. If you see valid patterns in the file then the issue is probably caused by a circular import.

I modified the method to be. Not exactly sure how you mean to use an if in this case.

@lru_cache.lru_cache(maxsize=None)
def get_resolver(urlconf=None):
    if urlconf is None:
        from django.conf import settings
        urlconf = getattr(settings, 'ROOT_URLCONF', None)
    return RegexURLResolver(r'^/', urlconf)

comment:6 Changed 4 years ago by Tim Graham

I mean something like this:

@register(Tags.urls)
def check_url_config(app_configs, **kwargs):
    from django.urls import get_resolver
    if getattr(settings, ROOT_URLCONF, None):
        resolver = get_resolver()
        return check_resolver(resolver)
    return []

comment:7 Changed 4 years ago by Buddy Lindsey

I see what you are after.

Just tried that and it works, is an easier solution too. You want me to fix it, add tests, and submit a PR? Or you?

comment:8 Changed 4 years ago by Tim Graham

You may do it. Please include a mention in the 1.9.2 release notes too.

comment:9 Changed 4 years ago by Tim Graham <timograham@…>

Resolution: fixed
Status: assignedclosed

In 731bdfe:

Fixed #26155 -- Skipped URL checks if no ROOTURL_CONF setting.

comment:10 Changed 4 years ago by Tim Graham <timograham@…>

In 3a063b0d:

[1.9.x] Fixed #26155 -- Skipped URL checks if no ROOTURL_CONF setting.

Backport of 731bdfe68ae8dd332702b1838db44f0efe433e2e from master

Note: See TracTickets for help on using tickets.
Back to Top