Django 1.7 getting ImportError, Cannot import name patterns - python

Right now when I attempt to do makemigrations, I get the infamous cannot import names pattern message, upon reviewing my urls.py file. I've been searching online for a potential solution given that I am working in Django 1.7, but I haven't found success. I still don't know why this is happening on my current version of django at all and any possible explanation on what could be causing it would be appreciated.
In my urls.py file I have
from django.conf.urls import patterns, include, url
from django.views.generic import RedirectView
from django.contrib import admin
admin.autodiscover()
from new_bridge import views
urlpatterns = patterns('',
url(r'^$', views.IndexView, name='index'),
url(r'^admin/import', views.myimport),
(r'^favicon\.ico$', RedirectView.as_view(url='/static/images/bridge_favicon.ico')),
url(r'^words_page_redirect/(?P<language>[a-zA-Z]+)/$', views.words_page_redirect),....
)
and the final line of the message I get on terminal is simply
File "/srv/bridge-repo/new_bridge/urls.py", line 1, in <module>
from django.conf.urls import patterns, include, url
Please let me know what other info I could provide to be of more help. Any input on what I could do to actually learn more about this issue, alongside resolve it, is appreciated.

In Django 1.7, you should be able to import patterns from django.conf.urls. Is it possible you are on a newer version of Django?
Also, the use of patterns is deprecated since Django 1.8, so you can actually just avoid using it and replace your code with
urlpatterns = [
url(r'^$', views.IndexView, name='index'),
url(r'^admin/import', views.myimport),
url(r'^favicon\.ico$', RedirectView.as_view(url='/static/images/bridge_favicon.ico')),
url(r'^words_page_redirect/(?P<language>[a-zA-Z]+)/$', views.words_page_redirect),....
]
Note: make sure that each entry is wrapped with the url function with this approach.

Related

ImportError: cannot import name 'url' from 'django.conf.urls' after upgrading to Django 4.0

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'),
]
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.
I think a quick fix to this problem is to do followings;
You can easily replace
from django.conf.urls import url
to this:
from django.urls import re_path as url
And keep the rest of code to be same as before.
(Thanks #Alasdair)
See in django version 4.0 it will not work.
So while installing Django in your Virtual Environment select this version
pip install django==3.2.10
This will definitely solve your error and in main urls.py do this:
from django.conf.urls import url
from django.urls import path,include

Trying to reach url with Path include() shows 404 in Django

first of I want to apologize if I use the wrong terms or words in my question. I'm completely new to Django and got only a few months of experience with python. I hope you can understand my question anyways. I also want to acknowledge the fact that I'm using some imports that are not needed here and might not be relevant to the latest version of Django, I'm starting to get lost in all the things I've tried from other threads to solve my problem.
I'm having some problems with showing a page from apps url.
I'm getting redirected to my homepage when trying to reach localhost:8000/articles (because /articles gives 404 error)
I'm not sure exactly what code I need to include here, so bear with me.
articles/urls.py and articles/views.py
from django.conf.urls import url
from django.urls import include, path
from django.conf.urls import include, url
from django.urls import path
from .import views
urlpatterns = [
path('^$', views.article_list),
]
from django.shortcuts import render
from django.http import HttpResponse
# views
def article_list(request):
return render(request, "articles/article_list.html")
The project's urls.py and project's views.py
from django.contrib import admin
from django.urls import path
from django.conf.urls import url, include
from django.urls import include, path
from django.conf.urls import include, url
from django.urls import path, re_path
from .import views
urlpatterns = [
path('admin/', admin.site.urls),
path('articles/', include('articles.urls')),
path('about/', views.about),
re_path('^.*$', views.homepage)
]
from django.http import HttpResponse
from django.shortcuts import render
#Views
def homepage(request):
# return HttpResponse('homepage')
return render(request, "homepage.html")
def about(request):
# return HttpResponse('about')
return render(request, "about.html")
Im getting no errors or such.
So, my question is - does anybody have a clue why /articles generate 404 error?
Thank you in advance.
Firstly, don't use ^$ with path(). You only use regular expressions with re_path.
path('', views.article_list),
Usually, /articles will be redirected to /articles/ with a trailing slash.
However, in your case, you have a catch-all pattern:
re_path('^.*$', views.homepage)
This matches /articles, so you see the home page. Note it's not redirected as you say in your answer, the browser bar will still show /articles.
Unless you have a really good reason to have the catch all, I suggest you remove it and change it to
re_path('^$', views.homepage),
or
path('', views.homepage),
That way, you'll see the homepage for localhost:8000, localhost:8000/articles will be redirected to localhost:8000/articles/, and you'll get a 404 for pages that don't exist, e.g. localhost:8000/art/
Just using a empty string '' instead of '^$:
urlpatterns = [
path('', views.article_list),
]
Take a look at the last example here: https://docs.djangoproject.com/en/3.1/topics/http/urls/#url-namespaces-and-included-urlconfs
*I don't know what django version are you using, but for regular expressions paths you should use re_path() https://docs.djangoproject.com/en/3.1/ref/urls/#django.urls.re_path

Django: Support for string view arguments to url() is deprecated and will be removed in Django 1.10

New python/Django user (and indeed new to SO):
When trying to migrate my Django project, I get an error:
RemovedInDjango110Warning: Support for string view arguments to url() is deprecated
and will be removed in Django 1.10 (got main.views.home). Pass the callable instead.
url(r'^$', 'main.views.home')
Apparently the second argument can't be a string anymore. I came to create this code as it is through a tutorial at pluralsight.com that is teaching how to use Django with a previous version (I'm currently working with 1.9). The teacher instructs us to create urlpatterns in urls.py from the views we create in apps. He teaches us to create a urlpattern such as the following:
from django.conf.urls import url
from django.contrib import admin
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', 'main.views.home')
]
to reference
def home(request):
return render(request, "main/home.html",
{'message': 'You\'ve met with a terrible fate, haven\'t you?'}) #this message calls HTML, not shown, not important for question
in the views.py of an app "main" that I created.
If this method is being deprecated, how do I pass the view argument not as a string? If I just remove the quotes, as shown in the documentation (https://docs.djangoproject.com/en/1.9/topics/http/urls/), I get an error:
NameError: name 'main' is not defined
I tried to "import" views or main using the code presented in this documentation:
from . import views
or
from . import main
which gave me:
ImportError: cannot import name 'views'
and
ImportError: cannot import name 'main'
I believe I've traced this down to an import error, and am currently researching that.
I have found the answer to my question. It was indeed an import error. For Django 1.10, you now have to import the app's view.py, and then pass the second argument of url() without quotes. Here is my code now in urls.py:
from django.conf.urls import url
from django.contrib import admin
import main.views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', main.views.home)
]
I did not change anything in the app or view.py files.
Props to #Rik Poggi for illustrating how to import in his answer to this question:
Django - Import views from separate apps
You should be able to use the following:
from django.conf.urls import url
from django.contrib import admin
from main import views
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^$', views.home)
]
I'm not absolutely certain what your directory structure looks like, but using a relative import such as from . import X is for when the files are in the same folder as each other.
You can use your functions by importing all of them to list and added each one of them to urlpatterns.
from django.conf.urls import url
from django.contrib import admin
from main.views import(
home,
function2,
function3,
)
urlpatterns = [
url(r'^admin/', admin.site.urls),
url(r'^home/$', home),
url(r'function2/^$', function2),
url(r'^$', function3),
]

Django urls.py import not detecting

I have a working api, and I am writing a UI to the API, as a separate application in the same project. My project urls.py looks like this
from django.conf.urls import *
import search
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
url(r'^api/search$', search.validation),
url(r'^ui', include('ui.urls')),
)
My UI app's urls.py looks like this
from django.conf.urls import *
import views
urlpatterns = patterns('',
(r'^ui/$', views.search_template),
)
However, when I am trying to access with my browser(domain.com:8000/ui), I am getting an error.
Using the URLconf defined in api.urls, Django tried these URL patterns, in this order:
^api/search$
^ui ^ui$
The current URL, ui, didn't match any of these.
But if I use the below mapping in the main project's urls.py, it works.
(r'^ui$', ui.views.user_template),
I tried clearing the urls.pyc to make sure it is not stale, but it still persists. Please let me know what am I doing wrong.
You shouldn't repeat ui regex in the app's urls.py:
urlpatterns = patterns('',
(r'^$', views.search_template),
)

No module named urls

I'm following the Django Tutorials, I'm at the end of part 3, at Decoupling the URLconfs, at http://docs.djangoproject.com/en/1.1/intro/tutorial03/#intro-tutorial03 and I'm getting a "No module named urls" error message.
When I change:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('mysite.polls.views',
(r'^polls/$', 'index'),
(r'^polls/(?P<poll_id>\d+)/$', 'detail'),
(r'^polls/(?P<poll_id>\d+)/results/$', 'results'),
(r'^polls/(?P<poll_id>\d+)/vote/$', 'vote'),
(r'^admin/', include(admin.site.urls)),
)
to:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^polls/', include('mysite.polls.urls')),
(r'^admin/', include(admin.site.urls)),
)
I changed include('mysite.polls.urls')), to include(mysite.polls.urls)),, but it still didn't work.
How to solve this problem?
UPDATE 2: at mysite/polls/urls.py is
from django.conf.urls.defaults import *
urlpatterns = patterns('mysite.polls.views',
(r'^$', 'index'),
(r'^(?P<poll_id>\d+)/$', 'detail'),
(r'^(?P<poll_id>\d+)/results/$', 'results'),
(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
UPDATE 4: the whole project is at
http://www.mediafire.com/?t1jvomjgjz1
I had a similar problem in my project root ... django complained that it couldn't find the module mysite.urls.
Turns out my ROOT_URLCONF variable in settings.py, which was set up using the default values, was set incorrect. Instead of "mysite.urls", it should have been simply "urls"
I changed it, and voila, it worked.
I can't re-produce the import error on my machine using your project files (Windows 7, Django 1.1.1, Python 2.6.4). Everything imported fine but the urls were not specified properly (like the tutorial shows). Fixing the code:
/mysite/urls.py:
from django.conf.urls.defaults import *
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
(r'^polls/', include('mysite.polls.urls')),
(r'^admin/', include(admin.site.urls)),
)
/mysite/polls/urls.py:
from django.conf.urls.defaults import *
urlpatterns = patterns('mysite.polls.views',
(r'^$', 'index'),
(r'^(?P<poll_id>\d+)/$', 'detail'),
(r'^(?P<poll_id>\d+)/results/$', 'results'),
(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
Visit http://127.0.0.1:8000/polls/ - I received a TemplateDoesNotExist exception because the template file is missing.
I'm afraid my answer might be to reboot and try it again. ;)
Is there an __init__.py inside mysite/polls/ directory?
I also had a weird problem with "No module named mysite.urls".
The admin site was down and the my whole site.
The solution, after a few hours of searching the web, was on my side : Django is caching some of the settings in a file that he knows from an environment variable.
I just closed my terminal in which i was doing the runnserver thing and opened a new one.
I did exactly the same thing. Python newbie mistake by reading ahead. I created a file call "polls.url" thinking it was some sort of special django template file.
I misunderstood the text:
"Now that we've decoupled that, we need to decouple the polls.urls URLconf by removing the leading "polls/" from each line, and removing the lines registering the admin site. Your polls.urls file should now look like this:
"
It should really read:
"Now that we've decoupled that, we need to decouple the polls.urls URLconf by removing the leading "polls/" from each line, and removing the lines registering the admin site. Your polls/urls.py file should now look like this:
"
Late to the party but I fixed my problem by correcting a typo inside settings.py INSTALLED_APPS. I had 'webbapp' instead of 'webapp' so you might want to check on that as well (for people still having the problem)
Like Ryan Bagwell, problem was (at least temporarily) solved by changing ROOT_URLCONF in myproject/myproject/settings.py.
I had to add "myproject" to ROOT_URLCONF to have there this:
"%s.myproject.urls" % PROJECT_APP

Categories

Resources