简体   繁体   English

如何在 Django 2.0 中解决这个 `ImportError: Cannot import name url`?

[英]How to solve this `ImportError: Cannot import name url` in Django 2.0?

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

And when I run the code python manage.py runserver I get the following error:当我运行代码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'

And mysite/urls.py :和 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')),
]

And blog/urls.py :和 blog/urls.py :

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

Sorry for the ginormous code, and the version of Django I am using here is 2.0.抱歉代码太长了,我在这里使用的 Django 版本是 2.0。

But then I tried using the version used in tutorial that is 1.11.8, I got the same error but the last line of the error says ImportError: cannot import name 'include' instead of ImportError: cannot import name 'url'但是后来我尝试使用教程中使用的版本 1.11.8,我得到了同样的错误,但错误的最后一行说ImportError: cannot import name 'include'而不是ImportError: cannot import name 'url'

I don't understand how to use the official documentation yet, I started Django yesterday itself.我还不明白如何使用官方文档,我昨天自己启动了 Django。 Sorry for the inconvenience.带来不便敬请谅解。

Danke.丹克。

blog/urls.py: blog / urls.py:

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

mysite/urls.py: 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')),
]

As suggested follow the documentation: 根据建议,遵循文档:

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

The new version of django does not allow use新版django不允许使用

from django.urls import include, url从 django.urls 导入包含,url

or或者

from django.conf.urls import url从 django.conf.urls 导入 url

may be you can use re_path也许你可以使用re_path

from django.urls import include, re_path

urlpatterns = [
    re_path(r'^index/$', views.index, name='index'),
    re_path(r'^bio/(?P<username>\w+)/$', views.bio, name='bio'),
    re_path(r'^weblog/', include('blog.urls')),
    ...
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM