在遵循Django教程时遇到错误

2024-06-02 05:59:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我对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

Tags: djangoinpyselfvenvslibpackagesline
3条回答

我认为您使用的是python2.x和django1.x,但是本教程是针对Django 2.x的,所以您需要更新到python3,并使用pip下载django2.1的最新版本,它可以正常工作。可能的原因可能是使用IDE下载Django和Python,因为有些IDE中的插件都是过时的版本。visualstudio和Intellij Idea插件是主要原因。我建议使用Django的集成命令工具编写VS代码。在

实际上,include存在于django.conf.urls

网址.py用下面的代码代替from django.urls import include

from django.conf.urls import include

由于您使用的是Python2.7,而且django 2.0不支持Python2.7(请参见release notes),所以我认为您使用的是旧版本的django。在

在这种情况下,您可以在这里找到include函数django.conf.urls

from django.conf.urls import include

docs开始:

Changed in Django 2.0: In older versions, this function is located in django.conf.urls. The old location still works for backwards compatibility.

因此,如果您使用Django 2.0,则可以使用这两种方法导入include:

^{pr2}$

但对于以前的版本,只有第一个是正确的。在

至于path,它在Django 2.0中绝对是新的。在旧版本中,您应该使用^{}

from django.conf.urls import url

相关问题 更多 >