ImportError: cannot import name 'url' from 'django.conf.urls' - django

ImportError: cannot import name 'url' from 'django.conf.urls' (/home/chams/pfe_project/CarApp/venv/lib/python3.8/site-packages/django/conf/urls/_init_.py)
I can't find any mistakes in my code !

django.conf.urls.url was deprecated and removed in Django 4.0.
Use django.urls.re_path instead.

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()
You will be using this,
from django.conf.urls import url
Instead use this:
from django.urls import re_path as url

for Django 4.0.
from django.urls import re_path as url

Related

ImportError: from django.urls import path is not working

What is the probelm ?I am getting lot of stress with this code.
MY CODE::::
from django.contrib import admin
from django.urls import path
from basicapp import views
urlpatterns = [
path('',views.index,name='index'),
path('admin/', admin.site.urls),
path('formpage/',views.form_name_view,name='form_name'),
]
PROBLEM///ERROR::::
from django.urls import path
ImportError: cannot import name path
django.urls.path is new in Django 2.0. Make sure you use Django 2.0 or if you have to stick to <2.0 use django.conf.urls.url.
Docs for path (2.0): https://docs.djangoproject.com/en/2.1/ref/urls/#path
Docs for url (<2.0): https://docs.djangoproject.com/en/1.11/ref/urls/#url
It helps to use an editor that manages imports for you like PyCharm or Visual Code or Vi with appropriate plugins or many other.

ImportError: cannot import name 'patterns'

I have a problem with Django, I created a 'login' app and added the URL on mysite/urls.py as below:
from django.conf.urls import include, patterns, url
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
url(r'^polls/', include('polls.urls')),
url(r'^user-auth/', include('user_auth.urls')),
url(r'^file-upload/', include('file_uploader.urls')),
url(r'^pagination/', include('pagination.urls')),
patterns('login.views',
url(r'^login/', 'loginView'),
url(r'^greeting/', 'formView'),
url(r'^logout/', 'logoutView')
)
]
However, when I started the server, I received the message on console as:
File "/home/win/Python/mysite/mysite/urls.py", line 16, in <module>
from django.conf.urls import include, patterns, url
ImportError: cannot import name 'patterns'
Do you meet any problem like this? and any resolution do you have to resolve it.
Please please help me.
Thanks
In the latest release of Django (as of this post), patterns is not used.
You can use re_path for the same effect. For Example:
from django.urls import include, re_path
from django.contrib import admin
from myapp.views import *
urlpatterns = [
re_path(r'^admin', include(admin.site.urls)),
re_path(r'^$', home, name='home'),
]
For more information please follow: Documentation
FYI patterns has been removed in Django 1.10. See release 1.10 notes:
https://docs.djangoproject.com/en/2.0/releases/1.10/
If you want to use earlier versions (but i don't see why you would want to do it) , anything below that i.e. 1.9 should be ok, but do note it has been slotted for deprecation since 1.8 i think.
And if you're using django, especially if you are new, I don't see why you would want to use your own login app. Django has a very mature and customizable auth backend. For starters, I strongly suggest you check it out. Useful examples of usage at https://djangobook.com/authentication-views/
If you are using the latest version of Django, then patterns is has been deprecated. You would simply use URL and/or Path depending on if you are on 1.11 or 2.0. If you require patterns, then you would need to downgrade to an earlier Django version.

No module named django.core.urlresovers in django 2.0a1

Have recently upgraded from django 1.11.7 to django 2.0a1 and now cannot import reverse_lazy
File "/home/silasi/Deprojecto/eljogo/jogos/views.py", line 8, in <module>
from django.core.urlresolvers import reverse_lazy
ModuleNotFoundError: No module named 'django.core.urlresolvers'
In Django 2.0 you must import:
from django.urls import reverse_lazy
Since version 1.10 django.core.urlresolvers was deprecated, change the import to
from django.urls import reverse_lazy
more information about 1.10 version,
more info about django.urls

ImportError: No module named defaults

I am using django 1.9 version and I wanted to implement ajax search in my application. In the documentation it is says to add the urls to the root url patterns.
url(r'^ajax_search/',include('ajax_search.urls')),`
Then I am getting an import error as follows:
File "/usr/local/lib/python2.7/dist-packages/django_ajax_search-1.5.1-py2.7.egg/ajax_search/urls.py", line 1, in <module>
from django.conf.urls.defaults import *
ImportError: No module named defaults
Can any one help me solve this issue?
django.conf.urls.defaults has been removed from Django 1.6 onwards.
django-ajax-search package was last updated in 2013. The package has not been updated for a long and will not work smoothly for Django 1.9
Either you can find another package or you can manually update it.
django.conf.urls.defaults is deprecated in Django 1.4, later removed in Django 1.6. Read this. And the package you are using has the urls not compatible with Django 1.9. According to the Django 1.9 documentation you should define your urls.py as,
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^articles/2003/$', views.special_case_2003),
url(r'^articles/([0-9]{4})/$', views.year_archive),
url(r'^articles/([0-9]{4})/([0-9]{2})/$', views.month_archive),
url(r'^articles/([0-9]{4})/([0-9]{2})/([0-9]+)/$', views.article_detail),
]
UPDATE:
You can modify your urls.py as below to make this working,
from django.conf.urls import url, include
from ajax_search import views as as_views
ajax_search_urlpatterns = [
url(r'^xhr_search$','as_views.xhr_search'),
url(r'^search/', 'as_views.search'),
]
urlpatterns = [
url(r'^ajax_search/',include(ajax_search_urlpatterns)),
]

"No module named simple" error in Django

ImportError at /
No module named simple
Django Version: 1.5.dev20120710212642
I installed latest django version. I am using
from django.views.generic.simple import redirect_to
in my urls.py. What is wrong? Is it deprecated?
Use class-based views instead of redirect_to as these function-based generic views have been deprecated.
Here is simple example of class-based views usage
from django.conf.urls import patterns, url, include
from django.views.generic import TemplateView
urlpatterns = patterns('',
(r'^about/', TemplateView.as_view(template_name="about.html")),
)
Update
If someone wants to redirect to a URL, Use RedirectView.
from django.views.generic import RedirectView
urlpatterns = patterns('',
(r'^one/$', RedirectView.as_view(url='/another/')),
)
this should work
from django.conf.urls import patterns
from django.views.generic import RedirectView
urlpatterns = patterns('',
url(r'some-url', RedirectView.as_view(url='/another-url/'))
)
Yes, the old function-based generic views were deprecated in 1.4. Use the class-based views instead.
And for the record (no relevant example currently in documentation), to use RedirectView with parameters:
from django.conf.urls import patterns, url
from django.views.generic import RedirectView
urlpatterns = patterns('',
url(r'^myurl/(?P<my_id>\d+)$', RedirectView.as_view(url='/another_url/%(my_id)s/')),
)
Please note that although the regex looks for a number (\d+), the parameter is passed as a string (%(my_id)s).
What is still unclear is how to use RedirectView with template_name in urls.py.