From django conf urls import url ошибка

django.conf.urls.url() was deprecated in Django 3.0, and is removed in Django 4.0+.

The easiest fix is to replace url() with re_path(). re_path uses regexes like url, so you only have to update the import and replace url with re_path.

from django.urls import include, re_path

from myapp.views import home

urlpatterns = [
    re_path(r'^$', home, name='home'),
    re_path(r'^myapp/', include('myapp.urls'),
]

Alternatively, you could switch to using path. path() does not use regexes, so you’ll have to update your URL patterns if you switch to path.

from django.urls import include, path

from myapp.views import home

urlpatterns = [
    path('', home, name='home'),
    path('myapp/', include('myapp.urls'),
]

If you have a large project with many URL patterns to update, you may find the django-upgrade library useful to update your urls.py files.

Django Forum

Loading

The ImportError: cannot import name ‘url’ from ‘django.conf.urls’ error occurs in Django when django.conf.urls.url()  module has been deprecated and removed in version 4 of Django, but you are still trying to import it.

To fix the ImportError: cannot import name ‘url’ from ‘django.conf.urls’ error, replace the url() with re_path().

The re_path uses regexes like url, so you only have to update the import and replace url with re_path.

from django.urls import re_path
from . import views

urlpatterns = [
 re_path(r'^$', views.index, name='index'),
 re_path(r'^about/$', views.about, name='about'),
 re_path(r'^contact/$', views.contact, name='contact'),
]

In this example, re_path() is imported from the django.urls module.

The urlpatterns list is where you define your URL patterns for the application.

The first argument for each re_path() function call is a regular expression pattern that matches the URL you want to handle.

The second argument is the view function that should be called when that URL is requested.

The third argument is an optional name for the URL pattern, which can be used for referencing it in other parts of the code.

An alternate way to fix the error

You can switch to using the path module. However, the path() does not use regexes, so you need to update your URL patterns if you switch to the path.

from django.urls import path, include
from . import views

urlpatterns = [
  path('create/file', views.create, include('app.urls')),
  path('edit/file', views.edit, include('app.urls')),
]

After updating this, you can use the django-upgrade helpful library to update your urls.py files.

You can also downgrade your Django version, but this is a temporary fix and not recommended because, at some point in the future, you need to upgrade your Django version to the latest version, so use this solution at your own risk.

Conclusion

The “ImportError: cannot import name ‘url’ from ‘django.conf.urls’” occurs because django.conf.urls.url() has been deprecated and removed in Django’s version 4, and t0 fix the ImportError, import, and use the re_path() method instead of url() method.

That’s it.

Hi,

I have installed django-markdownx version 3.0.1 on my app powered by Django 4.0.1 and I get an error when running:

python manage.py runserver

The error is the following:

  File "/home/matthieu/Documents/okumak_rebranded/okumak/urls.py", line 21, in <module>
    path('markdownx/', include('markdownx.urls')),
  File "/home/matthieu/Documents/venv/lib64/python3.10/site-packages/django/urls/conf.py", line 34, in include
    urlconf_module = import_module(urlconf_module)
  File "/usr/lib64/python3.10/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 "/home/matthieu/Documents/venv/lib64/python3.10/site-packages/markdownx/urls.py", line 7, in <module>
    from django.conf.urls import url
ImportError: cannot import name 'url' from 'django.conf.urls' (/home/matthieu/Documents/venv/lib64/python3.10/site-packages/django/conf/urls/__init__.py)

I guess it can be fixed by replacing the url() function by the re_path() function in the urls.py used by markdownx as follows:

from django.urls import re_path

from .views import (
    ImageUploadView,
    MarkdownifyView,
)


urlpatterns = [
    re_path(r'^upload/$', ImageUploadView.as_view(), name='markdownx_upload'),
    re_path(r'^markdownify/$', MarkdownifyView.as_view(), name='markdownx_markdownify'),
]

What do you think?

Thanks in advance,

Matthieu

Issue

After upgrading to Django 4.0, I get the following error when running python manage.py runserver

  ...
  File "/path/to/myproject/myproject/urls.py", line 16, in <module>
    from django.conf.urls import url
ImportError: cannot import name 'url' from 'django.conf.urls' (/path/to/my/venv/lib/python3.9/site-packages/django/conf/urls/__init__.py)

My urls.py is as follows:

from django.conf.urls

from myapp.views import home

urlpatterns = [
    url(r'^$', home, name="home"),
    url(r'^myapp/', include('myapp.urls'),
]

Solution

django.conf.urls.url() was deprecated in Django 3.0, and is removed in Django 4.0+.

The easiest fix is to replace url() with re_path(). re_path uses regexes like url, so you only have to update the import and replace url with re_path.

from django.urls import include, re_path

from myapp.views import home

urlpatterns = [
    re_path(r'^$', home, name='home'),
    re_path(r'^myapp/', include('myapp.urls'),
]

Alternatively, you could switch to using path. path() does not use regexes, so you’ll have to update your URL patterns if you switch to path.

from django.urls import include, path

from myapp.views import home

urlpatterns = [
    path('', home, name='home'),
    path('myapp/', include('myapp.urls'),
]

If you have a large project with many URL patterns to update, you may find the django-upgrade library useful to update your urls.py files.

Answered By – Alasdair

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Понравилась статья? Поделить с друзьями:
  • From datetime import datetime в чем ошибка
  • Friday the 13th the game ошибка подключения
  • Friday the 13th the game ошибка summercamp
  • Friamat prime eco ошибка 34
  • Freshtunes ошибка в названии альбома