如何解决这个`ImportError:无法在Django 2.0中导入名称url`?

时间:2017-12-07 21:16:33

标签: python django

好的,我正在关注这个https://tutorial.djangogirls.org/en/django_urls/教程。

当我运行代码python manage.py runserver时,我收到以下错误:

Performing system checks...

Unhandled exception in thread started by <function check_errors.<locals>.wrapper at 0x7faf45be1d08>
Traceback (most recent call last):
  File "/home/piyush/.virtualenvs/myenv/lib/python3.5/site-packages/django/utils/autoreload.py", line 225, in wrapper
    fn(*args, **kwargs)
  File "/home/piyush/.virtualenvs/myenv/lib/python3.5/site-packages/django/core/management/commands/runserver.py", line 120, in inner_run
    self.check(display_num_errors=True)
  File "/home/piyush/.virtualenvs/myenv/lib/python3.5/site-packages/django/core/management/base.py", line 364, in check
    include_deployment_checks=include_deployment_checks,
  File "/home/piyush/.virtualenvs/myenv/lib/python3.5/site-packages/django/core/management/base.py", line 351, in _run_checks
    return checks.run_checks(**kwargs)
  File "/home/piyush/.virtualenvs/myenv/lib/python3.5/site-packages/django/core/checks/registry.py", line 73, in run_checks
    new_errors = check(app_configs=app_configs)
  File "/home/piyush/.virtualenvs/myenv/lib/python3.5/site-packages/django/core/checks/urls.py", line 13, in check_url_config
    return check_resolver(resolver)
  File "/home/piyush/.virtualenvs/myenv/lib/python3.5/site-packages/django/core/checks/urls.py", line 23, in check_resolver
    return check_method()
  File "/home/piyush/.virtualenvs/myenv/lib/python3.5/site-packages/django/urls/resolvers.py", line 397, in check
    for pattern in self.url_patterns:
  File "/home/piyush/.virtualenvs/myenv/lib/python3.5/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/piyush/.virtualenvs/myenv/lib/python3.5/site-packages/django/urls/resolvers.py", line 536, in url_patterns
    patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
  File "/home/piyush/.virtualenvs/myenv/lib/python3.5/site-packages/django/utils/functional.py", line 36, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/piyush/.virtualenvs/myenv/lib/python3.5/site-packages/django/urls/resolvers.py", line 529, in urlconf_module
    return import_module(self.urlconf_name)
  File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/piyush/Desktop/Djangosite/mysite/urls.py", line 16, in <module>
    from django.urls import include, url
ImportError: cannot import name 'url'

和mysite / urls.py:

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


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

和blog / urls.py:

from django.conf.urls import url
from . import views
urlpatterns = [
    url(r'^$', views.post_list, name='post_list'),
]

对于巨大的代码感到抱歉,我在这里使用的Django版本是2.0。

但后来我尝试使用1.11.8教程中使用的版本,我得到了同样的错误,但错误的最后一行显示为ImportError: cannot import name 'include'而不是ImportError: cannot import name 'url'

我还不了解如何使用官方文档,我昨天开始使用Django。很抱歉给您带来不便。

丹科。

1 个答案:

答案 0 :(得分:5)

博客/ urls.py:

from django.urls import path
from . import views
urlpatterns = [
  path(r'^$', views.post_list, name='post_list'),
]

的mysite / urls.py:

from django.urls import include, path
from django.contrib import admin
urlpatterns = [
  path('admin/', admin.site.urls),
  path(r'', include('blog.urls')),
]

正如建议的那样遵循文档:

https://docs.djangoproject.com/en/2.0/intro/tutorial01/