【问题标题】:The included URLconf does not appear to have any patterns in it error包含的 URLconf 中似乎没有任何模式错误
【发布时间】:2018-02-01 14:16:29
【问题描述】:

我遇到了一个错误:

  ...
  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)

但同样的错误也会发生。我真的不明白为什么会发生这个错误。我应该如何解决这个问题?我的代码有什么问题?

【问题讨论】:

    标签: python django


    【解决方案1】:

    错误说,

    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

    【讨论】:

      【解决方案2】:

      尝试在你的代码下面注释

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

      这一定是循环导入问题的原因。如果不行请回复。

      【讨论】:

        【解决方案3】:

        尝试使用 路径('App/', 包含('App.urls')),

        【讨论】:

          猜你喜欢
          • 2018-10-22
          • 2014-11-22
          • 2019-08-21
          • 2018-06-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2022-08-21
          相关资源
          最近更新 更多