Django 4.0 url import error

Hello i just installed the new version of Django

and I have this problem with importing the url in urls.py file

from django.contrib import admin
from django.urls import path 
from django.conf.urls import  url
from ParamsMoni.views import index , get_data


urlpatterns = [
    path('admin/', admin.site.urls),
    url(r'^api/data/$', get_data , name='api-data'),
    path('', index, name='index'),
]

I can’t import from django.conf.urls import url don’t now why and it gives me this error!

File "C:\XPTANK\Tankparms\urls.py", line 18, in <module>
    from django.conf.urls import  url
ImportError: cannot import name 'url' from 'django.conf.urls' (C:\XPTANK\venv\lib\site-packages\django\conf\urls\__init__.py)

I am using Django==4.0

my best

The url function was deprecated in 3.1 and removed in 4.0.

1 Like

So think the path do the same function but when I try

`path(r'^api/data/$', get_data , name='api-data'),`

and in the views.py

def get_data(request , *args, **kwargs):
    data= {
        "sales": 100,
        "customerts": 10,
    }
    return JsonResponse(data)

and from the browser http://127.0.0.1:8000/api/data/

it gives me error Page not found (404)

it there any way to solve this?

Yes, did you read the documentation I linked to in my response?

nope, now I did :slight_smile:

so the solution is to replace the from django.conf.urls import url by

from django.urls import re_path

and

urlpatterns = [
    path('admin/', admin.site.urls),
    re_path(r'^api/data/$', get_data , name='api-data'),
    path('', index, name='index'),
]

it works perfectly

Many thanks you are the best :slight_smile:

Now that you’ve got it working, I’ll point out that using a regex in that situation doesn’t really provide any benefit. There are no variables in that path, nothing in the way of a “pattern” needing to be matched - just a straight string comparison.

You would be just as well served using path('api/data/', get_data , name='api-data'),

2 Likes

i’m also facing ModuleNotFoundError: No module named 'pillow’

Even i already installed pillow

This is a different problem. If you would like assistance with it, please open up a new topic in this category, thanks.

i have this issue too please; and my runserver shows errors:
rom django.conf.urls import include, url
from django.contrib import admin
#from django.urls import path , re_path

from clock import views
from clock import views
admin.autodiscover()

#from engrs.views import index , faq, forgot_password, change_password, profile, resume_preview ,setting, updateView, deleteView, login_view, logout_view, wrong_login, new_personnel, saved, units, courses, education, separation, discipline, trade, posting

from django.conf import settings
from django.conf.urls.static import static
#from engrs import views
#from django.urls import include, path

urlpatterns = [
url(r’^$’, ‘clock.views.index’, name=‘index’),
url( r’ admin/’, admin.site.urls), ]

===this is the error from the runserver terminal:
Performing system checks…

Exception in thread django-main-thread:
Traceback (most recent call last):
File “/usr/lib/python3.8/threading.py”, line 932, in _bootstrap_inner
self.run()
File “/usr/lib/python3.8/threading.py”, line 870, in run
self._target(*self._args, **self._kwargs)
File “/home/kola/Boom/GLUE/lib/python3.8/site-packages/django/utils/autoreload.py”, line 64, in wrapper
fn(*args, **kwargs)
File “/home/kola/Boom/GLUE/lib/python3.8/site-packages/django/core/management/commands/runserver.py”, line 134, in inner_run
self.check(display_num_errors=True)
File “/home/kola/Boom/GLUE/lib/python3.8/site-packages/django/core/management/base.py”, line 487, in check
all_issues = checks.run_checks(
File “/home/kola/Boom/GLUE/lib/python3.8/site-packages/django/core/checks/registry.py”, line 88, in run_checks
new_errors = check(app_configs=app_configs, databases=databases)
File “/home/kola/Boom/GLUE/lib/python3.8/site-packages/django/core/checks/urls.py”, line 14, in check_url_config
return check_resolver(resolver)
File “/home/kola/Boom/GLUE/lib/python3.8/site-packages/django/core/checks/urls.py”, line 24, in check_resolver
return check_method()
File “/home/kola/Boom/GLUE/lib/python3.8/site-packages/django/urls/resolvers.py”, line 480, in check
for pattern in self.url_patterns:
File “/home/kola/Boom/GLUE/lib/python3.8/site-packages/django/utils/functional.py”, line 49, in get
res = instance.dict[self.name] = self.func(instance)
File “/home/kola/Boom/GLUE/lib/python3.8/site-packages/django/urls/resolvers.py”, line 696, in url_patterns
patterns = getattr(self.urlconf_module, “urlpatterns”, self.urlconf_module)
File “/home/kola/Boom/GLUE/lib/python3.8/site-packages/django/utils/functional.py”, line 49, in get
res = instance.dict[self.name] = self.func(instance)
File “/home/kola/Boom/GLUE/lib/python3.8/site-packages/django/urls/resolvers.py”, line 689, in urlconf_module
return import_module(self.urlconf_name)
File “/usr/lib/python3.8/importlib/init.py”, line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “”, line 1014, in _gcd_import
File “”, line 991, in _find_and_load
File “”, line 975, in _find_and_load_unlocked
File “”, line 671, in _load_unlocked
File “”, line 848, in exec_module
File “”, line 219, in _call_with_frames_removed
File “/home/kola/Boom/GLUE/alarmclock/alarmclock/urls.py”, line 18, in
from django.conf.urls import include, url
ImportError: cannot import name ‘url’ from ‘django.conf.urls’ (/home/kola/Boom/GLUE/lib/python3.8/site-packages/django/conf/urls/init.py)

please, need your help

This is a different problem. If you would like assistance with it, please open up a new topic in this category, thanks.

And when posting code here, enclose each block of code between lines of three backtick - ` characters. This means you’ll have a line of ```, then your code, then another line of ```. This forces the forum software to keep your code properly formatted.