我对django和python都是全新的,目前我正在关注https://docs.djangoproject.com/en/2.0/intro/tutorial01/教程。
运行命令python manage.py runserver
时出现以下错误。有人可以帮忙吗?
Performing system checks...
Unhandled exception in thread started by <function wrapper at 0x102cf8140>
Traceback (most recent call last):
File "/Users/poojadeole/Desktop/venvs/env1/lib/python2.7/site-packages/django/utils/autoreload.py", line 228, in wrapper
fn(*args, **kwargs)
File "/Users/poojadeole/Desktop/venvs/env1/lib/python2.7/site-packages/django/core/management/commands/runserver.py", line 125, in inner_run
self.check(display_num_errors=True)
File "/Users/poojadeole/Desktop/venvs/env1/lib/python2.7/site-packages/django/core/management/base.py", line 359, in check
include_deployment_checks=include_deployment_checks,
File "/Users/poojadeole/Desktop/venvs/env1/lib/python2.7/site-packages/django/core/management/base.py", line 346, in _run_checks
return checks.run_checks(**kwargs)
File "/Users/poojadeole/Desktop/venvs/env1/lib/python2.7/site-packages/django/core/checks/registry.py", line 81, in run_checks
new_errors = check(app_configs=app_configs)
File "/Users/poojadeole/Desktop/venvs/env1/lib/python2.7/site-packages/django/core/checks/urls.py", line 16, in check_url_config
return check_resolver(resolver)
File "/Users/poojadeole/Desktop/venvs/env1/lib/python2.7/site-packages/django/core/checks/urls.py", line 26, in check_resolver
return check_method()
File "/Users/poojadeole/Desktop/venvs/env1/lib/python2.7/site-packages/django/urls/resolvers.py", line 254, in check
for pattern in self.url_patterns:
File "/Users/poojadeole/Desktop/venvs/env1/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/poojadeole/Desktop/venvs/env1/lib/python2.7/site-packages/django/urls/resolvers.py", line 405, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "/Users/poojadeole/Desktop/venvs/env1/lib/python2.7/site-packages/django/utils/functional.py", line 35, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "/Users/poojadeole/Desktop/venvs/env1/lib/python2.7/site-packages/django/urls/resolvers.py", line 398, in urlconf_module
return import_module(self.urlconf_name)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/Users/poojadeole/Desktop/projectdjango/mysite/mysite/urls.py", line 16, in <module>
from django.urls import include, path
ImportError: cannot import name include
答案 0 :(得分:3)
django.conf.urls
在 urls.py 中代替from django.urls import include
写下代码
from django.conf.urls import include
答案 1 :(得分:0)
由于您使用的是python 2.7,并且事实上django 2.0不支持python 2.7(请参阅release notes),我想您正在使用旧版本的Django。
在这种情况下,您可能会在include
找到django.conf.urls
函数:
from django.conf.urls import include
来自docs:
的Qoute在Django 2.0中更改: 在旧版本中,此函数位于django.conf.urls中。旧位置仍可用于向后兼容。
因此,如果您使用Django 2.0,则可以使用以下两种方法导入include:
from django.conf.urls import include
from django.urls import include
但对于以前的版本,只有第一个是正确的。
至于path,它在Django 2.0中是全新的。在旧版本中,您应该使用url
:
from django.conf.urls import url
答案 2 :(得分:0)
我认为您使用的是Python 2.x和Django 1.x,但本教程适用于Django 2.x,因此您需要更新至Python 3并使用pip下载最新的Django 2.1,它可以正常工作。可能的原因可能是使用IDE下载Django和Python,因为某些IDE中的插件坚持使用过时的版本。 Visual Studio和Intellij Idea插件是主要原因。我建议将VS Code与Django的集成命令工具一起使用。