'ImportError: cannot import name 'url' from 'django.conf.urls django #Python #Django #VisualStudioCode
I have been trying to write a simple django website. Unfortunately I am having errors with trying to import bayaan.urls (the app) into the urls.py(of the project).
This is the error:
(test) C:\Users\---\Documents\------\Django\apps\School>python manage.py runserver
Watching for file changes with StatReloader
Performing system checks...
Exception in thread django-main-thread:
Traceback (most recent call last):
File "C:\Users\---\AppData\Local\Programs\Python\Python310\lib\threading.py", line 1009, in _bootstrap_inner
self.run()
File "C:\Users\---\AppData\Local\Programs\Python\Python310\lib\threading.py", line 946, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\---\Envs\test\lib\site-packages\django\utils\autoreload.py", line 64, in wrapper
fn(*args, **kwargs)
File "C:\Users\---\Envs\test\lib\site-packages\django\core\management\commands\runserver.py", line 124, in inner_run
self.check(display_num_errors=True)
File "C:\Users\---\Envs\test\lib\site-packages\django\core\management\base.py", line 438, in check
all_issues = checks.run_checks(
File "C:\Users\---\Envs\test\lib\site-packages\django\core\checks\registry.py", line 77, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File "C:\Users\---\Envs\test\lib\site-packages\django\core\checks\urls.py", line 13, in check_url_config
return check_resolver(resolver)
File "C:\Users\---\Envs\test\lib\site-packages\django\core\checks\urls.py", line 23, in check_resolver
return check_method()
File "C:\Users\---\Envs\test\lib\site-packages\django\urls\resolvers.py", line 446, in check
for pattern in self.url_patterns:
File "C:\Users\---\Envs\test\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\---\Envs\test\lib\site-packages\django\urls\resolvers.py", line 632, in url_patterns
patterns = getattr(self.urlconf_module, "urlpatterns", self.urlconf_module)
File "C:\Users\---\Envs\test\lib\site-packages\django\utils\functional.py", line 48, in __get__
res = instance.__dict__[self.name] = self.func(instance)
File "C:\Users\---\Envs\test\lib\site-packages\django\urls\resolvers.py", line 625, in urlconf_module
return import_module(self.urlconf_name)
File "C:\Users\---\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "C:\Users\---\Documents\----\Django\apps\School\School\urls.py", line 20, in <module>
path('bayaan/', include('bayaan.urls')),
File "C:\Users\---\Envs\test\lib\site-packages\django\urls\conf.py", line 34, in include
urlconf_module = import_module(urlconf_module)
File "C:\Users\---\AppData\Local\Programs\Python\Python310\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 883, in exec_module
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
File "C:\Users\---\Documents\---\Django\apps\School\bayaan\urls.py", line 1, in <module>
from django.conf.urls import url
ImportError: cannot import name 'url' from 'django.conf.urls' (C:\Users\---\Envs\test\lib\site-packages\django\conf\urls\__init__.py)
This is my code (in bayaan(the app)):
from django.conf.urls import url
from django.urls import path
from . import views
urlpatterns = [
path('',views.home, name='home')]
This is the code in the School folder(The project):
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('bayaan/', include('bayaan\urls.py')),
path('admin/', admin.site.urls),
]
I've tried using re_path with no luck. By the way, I'm using python 3.10.1 as well as django 4.0.1. My code editor is Visual Studio Code.
ps. I'm using windows.
Solution 1:[1]
The url() method was removed in Django 4.0. Look at the release notes here: https://docs.djangoproject.com/en/4.0/releases/4.0/#features-removed-in-4-0
- django.conf.urls.url() is removed
But in your case, you're not using the url method only path (supported in Django 4.0), so only remove the import line from your code
from django.conf.urls import url
For more details how Django processes a request (using path and re-path methods) see the documentation: https://docs.djangoproject.com/en/4.0/topics/http/urls/#how-django-processes-a-request
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ZapSys |
