Django 4.0 from django.conf.urls import url


from django.conf.urls import url 不能使用,無法使用

Cannot find reference 'url' in '__init__.py'

url 已在Django 4.0中刪除。請查看此處的發行說明:

https://docs.djangoproject.com/pl/4.0/releases/4.0/#features-removed-in-4-0

django.conf.urls.url() is removed.

 

解決方法;

使用 re_path 替代 url

 

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.

 

REF

https://forum.djangoproject.com/t/django-4-0-url-import-error/11065/3

https://stackoverflow.com/questions/70319606/importerror-cannot-import-name-url-from-django-conf-urls-after-upgrading-to


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM