使用 include('admin.site.urls') 时出错:不支持将 3 元组传递给 include()

Posted

技术标签:

【中文标题】使用 include(\'admin.site.urls\') 时出错:不支持将 3 元组传递给 include()【英文标题】:Error when using include('admin.site.urls'): Passing a 3-tuple to include() is not supported使用 include('admin.site.urls') 时出错:不支持将 3 元组传递给 include() 【发布时间】:2018-06-20 13:28:24 【问题描述】:

我是 Python 的新手,我正在使用 Lynda 上的视频教程来帮助我构建社交 WebApp 的框架。我正在尝试使用 cmd 中的 python manage.py runserver 从 cmd 运行服务器,但是,我一直遇到此错误消息。

CMD 提示错误

Traceback (most recent call last):

File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\core\management\commands\runserver.py", line 121, in inner_run
    self.check(display_num_errors=True)
  File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\core\management\base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\core\management\base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\core\checks\registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\urls\resolvers.py", line 536, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\utils\functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\urls\resolvers.py", line 529, in urlconf_module
    return import_module(self.urlconf_name)
  File "C:\Users\Kelechi\AppData\Local\Programs\Python\Python35\lib\importlib\__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "C:\Windows\SysWOW64\bookmarks\bookmarks\urls.py", line 20, in <module>
    url(r'^admin/', include(admin.site.urls)),
  File "C:\Users\Kelechi\AppData\Roaming\Python\Python35\site-packages\django\urls\conf.py", line 27, in include
    'provide the namespace argument to include() instead.' % len(arg)
django.core.exceptions.ImproperlyConfigured: Passing a 3-tuple to include() is not supported. Pass a 2-tuple containing the list of patterns and app_name, and provide the namespace argument to include() instead.

我的 urls.py 看起来像这样:

from django.contrib import admin

urlpatterns = [
    url(r'^admin/', include(admin.site.urls)),
    ...
]

【问题讨论】:

显示 Django 代码本身的摘录毫无意义。您应该显示 您的 代码,在本例中是使用 include() 的 URL 配置。 如果您正在学习教程,使用与教程编写的相同版本的 Django 会容易得多,否则您会遇到类似这样的令人困惑的错误。如果它是为 Django 1.9/1.10(不再受支持)编写的,那么最好使用 Django 1.11 而不是 Django 2.0。 【参考方案1】:

在 Django 2.0 中,您不能再使用 include(admin.site.urls) (release notes)。只需改用admin.site.urls

from django.contrib import admin

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    ...
]

【讨论】:

请注意,这仅适用于 admin.site.urls。其他所有内容(例如 wagtail 和您自己的应用程序 url)仍然需要 include(xxx)。【参考方案2】:

从 Django 版本 2.0 开始,文档清除了这一点:

何时使用include()

当您包含其他 URL 模式时,您应该始终使用 include()admin.site.urls 是唯一的例外。

【讨论】:

以上是关于使用 include('admin.site.urls') 时出错:不支持将 3 元组传递给 include()的主要内容,如果未能解决你的问题,请参考以下文章

使用 %include 运行 SAS 脚本

include指令

include与require的区别

include的使用

防止实体框架在使用 Include 时添加 ORDER BY

libevent 框架使用例子