简体   繁体   中英

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.

And when I run the code python manage.py runserver I get the following error:

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 :

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 :

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.

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'

I don't understand how to use the official documentation yet, I started Django yesterday itself. Sorry for the inconvenience.

Danke.

blog/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')),
]

As suggested follow the documentation:

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

The new version of django does not allow use

from django.urls import include, url

or

from django.conf.urls import url

may be you can use 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')),
    ...
]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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