Python Django manage.py runserver太多值无法解压缩将3元组传递给include()不受支持

时间:2018-04-30 19:16:15

标签: python django

尝试运行

带有几乎全新的django项目的虚拟环境中的python manage.py runserver

然后得到这个长错误

(venv) ugps-MacBook-4:mysite gust$ python manage.py runserver
Performing system checks...

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x10d09b268>
Traceback (most recent call last):
  File "/Users/gust/GoogleDrive/code/python/django/venv/lib/python3.6/site-packages/django/urls/conf.py", line 17, in include
    urlconf_module, app_name = arg
ValueError: too many values to unpack (expected 2)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/gust/GoogleDrive/code/python/django/venv/lib/python3.6/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/Users/gust/GoogleDrive/code/python/django/venv/lib/python3.6/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
    self.check(display_num_errors=True)
  File "/Users/gust/GoogleDrive/code/python/django/venv/lib/python3.6/site-packages/django/core/management/base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "/Users/gust/GoogleDrive/code/python/django/venv/lib/python3.6/site-packages/django/core/management/base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "/Users/gust/GoogleDrive/code/python/django/venv/lib/python3.6/site-packages/django/core/checks/registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/Users/gust/GoogleDrive/code/python/django/venv/lib/python3.6/site-packages/django/core/checks/urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "/Users/gust/GoogleDrive/code/python/django/venv/lib/python3.6/site-packages/django/core/checks/urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "/Users/gust/GoogleDrive/code/python/django/venv/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/gust/GoogleDrive/code/python/django/venv/lib/python3.6/site-packages/django/urls/resolvers.py", line 536, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/Users/gust/GoogleDrive/code/python/django/venv/lib/python3.6/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/Users/gust/GoogleDrive/code/python/django/venv/lib/python3.6/site-packages/django/urls/resolvers.py", line 529, in urlconf_module
    return import_module(self.urlconf_name)
  File "/Users/gust/GoogleDrive/code/python/django/venv/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/Users/gust/GoogleDrive/code/python/django/mysite/mysite/urls.py", line 9, in <module>
    url(r'^admin/', include(admin.site.urls)),
  File "/Users/gust/GoogleDrive/code/python/django/venv/lib/python3.6/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.

我的python,django和pip版本是 python 3.6.4 (2,0,4,&#39;最终&#39;,0) 点10.0.1

使用具有2.7的本机python版本的mac os high sierra,但由于我使用的是虚拟环境,因此不应该成为问题

urls.py

from django.conf.urls import include, url
from django.contrib import admin

urlpatterns = [
    # Examples:
    # url(r'^$', 'mysite.views.home', name='home'),
    # url(r'^blog/', include('blog.urls')),

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

1 个答案:

答案 0 :(得分:9)

在Django 1.9中更改: 在以前的版本中,您可以将admin.site.urls传递给include() 添加django admin app的正确方法是

from django.contrib import admin

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