ImportError: from django.urls import path is not working - django

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.

Related

Django is rendered blank pages after updating path to re_path

Im new to Django but after solving the orginal issues and changing path to re_path in all my URL files Django now starts the server with no issues. The URLs load but all pages are blank except the home page.
`
from django.urls import re_path
from django.conf.urls import include
from django.contrib import admin
#from django.urls import path
from django.conf.urls.static import static
from django.conf import settings
urlpatterns = [
re_path('admin/', admin.site.urls),
re_path('', include('home.urls')),
re_path("users/", include("django.contrib.auth.urls")),
re_path("users/", include("users.urls")),
re_path("accounts/", include("accounts.urls")),
My console shows no errors so I am unsure what I am doing wrong.
`
The fix was done by updating all url.py files.
1st Remove
from django.conf.urls import url
replace with
from django.urls import path
if any paths start with url simply change to path
for example
url('posts/',views.posts, name = 'posts'),
would become
path('posts/',views.posts, name = 'posts'),
save the files and rerun the server.

Improperly Configured urls.py during Django deployment (Django 2.1)

This is my first time deploying Django. My app runs fine locally, but when I deploy, I get this error:
ImproperlyConfigured at /admin/
The included URLconf module 'search.urls' from '/home/imeaytbc/myproject/search/urls.py' does not
appear to have any patterns in it. If you see valid patterns in the
file then the issue is probably caused by a circular import.
My urls.py file is exactly the same as the one run on my computer:
from django.urls import path
from . import views
app_name = 'search'
urlpatterns = [
path('', views.query_search, name='query_search'),
path('article/<int:ArticleID>/', views.article_detail, name='article_detail')
]
Is there anything I need to change in regards to deployment? All the changes I made to my files regarding deployment are about static and media file directories. What else do I need to change for deployment? As far as I am aware, I have uploaded all files to the hosting server and the app shouldn't be missing any file.
EDIT: added main urls.py
from django.contrib import admin
from django.urls import include, path
from django.conf import settings
from django.conf.urls.static import static
from django.views.generic import RedirectView
urlpatterns = [
path('admin/', admin.site.urls),
path('search/', include('search.urls')),]

No module named 'django.urls'

I have a problem:
ImportError at / No module named 'django.urls'
urls.py:
from django.contrib import admin
from django.urls import path
from polls.views import *
urlpatterns = [
path('', EnterPage, name='home'),
path('admin/', admin.site.urls),
path('main/',EnterPage),
path('login/',loginn),
path('admin-panel/',adminPan),
path('control-users/',panel),
path('menu/',menu),
path('perspage/',lk),
path('spisok-zakazov/',product),
path('info/',info),
path('task-panel/', tasks),
path('tasks/',earncoin)
]
I don't understand why I am getting this error.
Maybe you're using an older django version. You can check your version using:
python -c "import django; print(django.get_version())"
If you look at the django documentation there are several modifications including the urls module.
Version >= 2.0: https://docs.djangoproject.com/en/2.0/ref/urls/
from django.urls import include, path
Version < 2.0: https://docs.djangoproject.com/en/1.11/ref/urls/
from django.conf.urls import include, url
Django 2.0 release notes:
https://docs.djangoproject.com/pt-br/2.1/releases/2.0/#simplified-url-routing-syntax

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.

Django URL mapping - NameError: name X is not defined

[A similar question was asked, but not marked as answered, here. I considered continuing that thread but the website told me I'm only supposed to post an answer, so it seems I have to start a new topic.] I'm trying to follow this tutorial and I'm having problems with the URL mapping. Specifically with the part described as "So best practice is to create an “url.py” per application and to include it in our main projects url.py file". The relevant, I hope, part of the folder structure, which arose by following steps of the tutorial to the letter (if possible; usage of the 'patterns' module was impossible for example) and using Django 1.10 is the following:
myproject/
myapp/
urls.py
views.py
myproject/
urls.py
The myproject/urls.py is as follows:
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
from myapp.views import hello
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include(myapp.urls)),
]
The myapp/urls.py is as follows:
from django.conf.urls import include, url
urlpatterns = [
url(r'^hello/', myapp.views.hello),
]
The myapp/views.py is as follows:
from django.shortcuts import render
def hello(request):
return render(request, "hello.html", {})
However, running 'python manage.py runserver' results in the following error:
url(r'^myapp/', include(myapp.urls)),
NameError: name 'myapp' is not defined
INSTALLED_APPS in settings.py contains 'myapp'.
I'd be greatful for any tips on how to deal with the NameError! [Or any tips whatsoever that anyone might consider to be helpful!]
You have the NameError because you are referencing myapp in myproject/urls.py but haven't imported it.
The typical approach in Django is to use a string with include, which means that the import is not required.
url(r'^myapp/', include('myapp.urls')),
Since you have move the hello URL pattern into myapp/urls.py, you can remove from myapp.views import hello from myproject/urls.py.
Once you've made that change, you will get another NameError in myapp/urls.py. In this case, a common approach is to use a relative import for the app's views.
from django.conf.urls import url
from . import views
urlpatterns = [
url(r'^hello/$', views.hello),
]
Make sure you have imported following modules to urls.py.
from django.conf.urls import url
from django.contrib import admin
in django 2.0
use these
from django.contrib import admin
from django.urls import path
from first_app import views
urlpatterns = [
path('',views.index, name="index"),
path('admin/', admin.site.urls),
]
your app URL has to be a string
so, here is how the code should look like.
from django.conf.urls import include, url
from django.contrib import admin
admin.autodiscover()
from myapp.views import hello
urlpatterns = [
url(r'^admin/', include(admin.site.urls)),
url(r'^myapp/', include('myapp.urls')),
]
also, note that from python 2 upward the regular expression is not needed.
change URL to path
from django.conf.URLs import include path
from Django.contrib import admin
admin.autodiscover()
from myapp.views import hello
urlpatterns = [
path('^admin/', include(admin.site.urls)),
path('^myapp/', include('myapp.urls')),
]
In Django 2.1.7 here is the default urls .py file
from django.contrib import admin
from django.urls import path
urlpatterns = [
path('admin/', admin.site.urls),
]
so we need to add this line as well
from django.conf.urls import url
I have followed #Alasdair answers
You have the NameError because you are referencing myapp in myproject/urls.py but haven't imported it.
The typical approach in Django is to use a string with include, which
means that the import is not required.
Unfortunately, it didn't work out(I still got the name X is not defined error). Here is how I do it.
from django.contrib import admin
from django.urls import include
from django.conf.urls import url
from article import urls as article_users
from article import urls as user_urls
urlpatterns = [
path('admin/', admin.site.urls),
path('api/article/', include(article_users)),
path('api/user/', include(user_urls)),
]
Before using the URL command be sure to first import the url from the module Urls. Then try using the runserver.
from django.conf.urls import url
from django.contrib import admin
from django.urls import path