包含的URLconf似乎没有任何模式错误

时间:2018-02-01 14:16:30

标签: python django

我收到了一个错误:

  ...
  File "/Users/xxx/anaconda/envs/py36/lib/python3.6/site-packages/django/urls/resolvers.py", line 545, in url_patterns
    raise ImproperlyConfigured(msg.format(name=self.urlconf_name))
django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'django.urls' from '/Users/xxx/anaconda/envs/py36/lib/python3.6/site-packages/django/urls/__init__.py'>' 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.

我最近从Django 1.8更新到2.0,所以我认为这就是错误发生的原因。我认为urls.py导致了这个错误,所以我把它重写为

from django.urls import include, path

app_name = 'App'

urlpatterns = [
    path('admin/', admin.site.urls),
    path('app/', include('app.urls')),

 ] +static(settings.MEDIA_URL,document_root=settings.MEDIA_ROOT)

但同样的错误发生了。我真的不明白为什么会发生这种错误。我该怎么解决这个问题?我的代码出了什么问题?

3 个答案:

答案 0 :(得分:0)

错误说,

django.core.exceptions.ImproperlyConfigured: The included URLconf '<module 'django.urls' from '/Users/xxx/anaconda/envs/py36/lib/python3.6/site-packages/django/urls/__init__.py'>' 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.

如果您的envs/py36/lib/python3.6/site-packages/django/urls/__init__.py内容如下,请检查一下:

from .base import (
    clear_script_prefix, clear_url_caches, get_script_prefix, get_urlconf,
    is_valid_path, resolve, reverse, reverse_lazy, set_script_prefix,
    set_urlconf, translate_url,
)
from .exceptions import NoReverseMatch, Resolver404
from .resolvers import (
    LocaleRegexProvider, LocaleRegexURLResolver, RegexURLPattern,
    RegexURLResolver, ResolverMatch, get_ns_resolver, get_resolver,
)
from .utils import get_callable, get_mod_func

__all__ = [
    'LocaleRegexProvider', 'LocaleRegexURLResolver', 'NoReverseMatch',
    'RegexURLPattern', 'RegexURLResolver', 'Resolver404', 'ResolverMatch',
    'clear_script_prefix', 'clear_url_caches', 'get_callable', 'get_mod_func',
    'get_ns_resolver', 'get_resolver', 'get_script_prefix', 'get_urlconf',
    'is_valid_path', 'resolve', 'reverse', 'reverse_lazy', 'set_script_prefix',
    'set_urlconf', 'translate_url',
]

如果没有那就是问题,否则你在某处使用循环导入,即在file1.py中你正在进行from file2 import var2而在file2.py中你正在使用循环导入。重新做from file1 import var1

答案 1 :(得分:0)

尝试在下面对您的代码进行评论

path('app/', include('app.urls')),

这必须是循环导入问题的原因。如果不起作用,请回复。

答案 2 :(得分:0)

尝试使用 path('App /',include('App.urls')),