5. Django 2.1.7 runserver启动直接报错

2019-06-03  本文已影响0人  Devops海洋的渔夫

在其他办公电脑创建的Django项目 2.2.1 版本都可以直接 runserver 启动服务正常。
但是本地创建的项目,只要执行python3 manage.py runserver 就直接报错。

错误详细日志

F:\pythonProject\mysite>python3 manage.py runserver
Performing system checks...

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x000001E95E2177B8>
Traceback (most recent call last):
  File "G:\Python3\Python37\lib\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 "G:\Python3\Python37\lib\site-packages\django\utils\autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "G:\Python3\Python37\lib\site-packages\django\core\management\commands\runserver.py", line 117, in inner_run
    self.check(display_num_errors=True)
  File "G:\Python3\Python37\lib\site-packages\django\core\management\base.py", line 379, in check
    include_deployment_checks=include_deployment_checks,
  File "G:\Python3\Python37\lib\site-packages\django\core\management\base.py", line 366, in _run_checks
    return checks.run_checks(**kwargs)
  File "G:\Python3\Python37\lib\site-packages\django\core\checks\registry.py", line 71, in run_checks
    new_errors = check(app_configs=app_configs)
  File "G:\Python3\Python37\lib\site-packages\django\core\checks\urls.py", line 40, in check_url_namespaces_unique
    all_namespaces = _load_all_namespaces(resolver)
  File "G:\Python3\Python37\lib\site-packages\django\core\checks\urls.py", line 57, in _load_all_namespaces
    url_patterns = getattr(resolver, 'url_patterns', [])
  File "G:\Python3\Python37\lib\site-packages\django\utils\functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "G:\Python3\Python37\lib\site-packages\django\urls\resolvers.py", line 533, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "G:\Python3\Python37\lib\site-packages\django\utils\functional.py", line 37, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "G:\Python3\Python37\lib\site-packages\django\urls\resolvers.py", line 526, in urlconf_module
    return import_module(self.urlconf_name)
  File "G:\Python3\Python37\lib\importlib\__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
  File "<frozen importlib._bootstrap>", line 983, in _find_and_load
  File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "F:\pythonProject\mysite\mysite\urls.py", line 20, in <module>
    url(r'^admin/', include(admin.site.urls)),
  File "G:\Python3\Python37\lib\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.

F:\pythonProject\mysite>

奇怪就在于我启动其他电脑创建的项目完全是没问题的,就是本机新建的项目会报错。而且是一句代码都没写。

导致错误是Django默认创建的urls.py存在问题

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

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

解决问题

最后调试了下,发现Django 创建项目后的 urls.py 文件有问题,启动就直接报错。修改如下即可解决问题。

from django.contrib import admin
from django.urls import include, path # 增加导入include方法

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

启动服务成功,如下:

F:\pythonProject\mysite>python3 manage.py runserver
Performing system checks...

System check identified no issues (0 silenced).

You have 15 unapplied migration(s). Your project may not work properly until you apply the migrations for app(s): admin, auth, contenttypes, sessions.
Run 'python manage.py migrate' to apply them.
June 03, 2019 - 22:50:30
Django version 2.1.7, using settings 'mysite.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREAK.

上一篇下一篇

猜你喜欢

热点阅读