在其他辦公電腦創(chuàng)建的Django項(xiàng)目 2.2.1 版本都可以直接 runserver 啟動(dòng)服務(wù)正常。
但是本地創(chuàng)建的項(xiàng)目,只要執(zhí)行python3 manage.py runserver 就直接報(bào)錯(cuò)。
錯(cuò)誤詳細(xì)日志
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>
奇怪就在于我啟動(dòng)其他電腦創(chuàng)建的項(xiàng)目完全是沒問題的,就是本機(jī)新建的項(xiàng)目會(huì)報(bào)錯(cuò)。而且是一句代碼都沒寫。
導(dǎo)致錯(cuò)誤是Django默認(rèn)創(chuàng)建的urls.py存在問題

from django.conf.urls import include, url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
]
解決問題
最后調(diào)試了下,發(fā)現(xiàn)Django 創(chuàng)建項(xiàng)目后的 urls.py 文件有問題,啟動(dòng)就直接報(bào)錯(cuò)。修改如下即可解決問題。
from django.contrib import admin
from django.urls import include, path # 增加導(dǎo)入include方法
urlpatterns = [
path('admin/', admin.site.urls),
]
啟動(dòng)服務(wù)成功,如下:
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.