My website, which was working before, suddenly started breaking with the error
ImproperlyConfigured at / The included urlconf resume.urls doesn't
have any patterns in it
The project base is called resume. In settings.py I have set
ROOT_URLCONF = 'resume.urls'
Here's my resume.urls, which sits in the project root directory.
from django.conf.urls.defaults import *
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Example:
# (r'^resume/', include('resume.foo.urls')),
# Uncomment the admin/doc line below and add 'django.contrib.admindocs'
# to INSTALLED_APPS to enable admin documentation:
(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# Uncomment the next line to enable the admin:
(r'^admin/', include(admin.site.urls)),
(r'^accounts/login/$', 'django.contrib.auth.views.login'),
#(r'^employer/', include(students.urls)),
(r'^ajax/', include('urls.ajax')),
(r'^student/', include('students.urls')),
(r'^club/(?P<object_id>\d+)/$', 'resume.students.views.club_detail'),
(r'^company/(?P<object_id>\d+)/$', 'resume.students.views.company_detail'),
(r'^program/(?P<object_id>\d+)/$', 'resume.students.views.program_detail'),
(r'^course/(?P<object_id>\d+)/$', 'resume.students.views.course_detail'),
(r'^career/(?P<object_id>\d+)/$', 'resume.students.views.career_detail'),
(r'^site_media/(?P<path>.*)$', 'django.views.static.serve', {'document_root': 'C:/code/django/resume/media'}),
)
I have a folder called urls and a file ajax.py inside. (I also created a blank init.py in the same folder so that urls would be recognized.) This is ajax.py.
from django.conf.urls.defaults import *
urlpatterns = patterns('',
(r'^star/(?P<object_id>\d+)$', 'resume.students.ajax-calls.star'),
)
Anyone know what's wrong? This is driving me crazy.
Thanks,
TL;DR: You probably need to use reverse_lazy() instead of reverse()
If your urls.py imports a class-based view that uses reverse(), you will get this error; using reverse_lazy() will fix it.
For me, the error
The included urlconf project.urls doesn't have any patterns in it
got thrown because:
project.urls imported app.urls
app.urls imported app.views
app.views had a class-based view that used reverse
reverse imports project.urls, resulting in a circular dependency.
Using reverse_lazy instead of reverse solved the problem: this postponed the reversing of the url until it was first needed at runtime.
Moral: Always use reverse_lazy if you need to reverse before the app starts.
Check your patterns for include statements that point to non-existent modules or modules that do not have a urlpatterns member. I see that you have an include('urls.ajax') which may not be correct. Should it be ajax.urls?
check for correct variable name in your app, if it is "
urlpatterns
" or any thing else.
Correcting name helped me
IN my case I got this error during deployment.
Apache kept giving me the "AH01630: client denied by server configuration" error.
This indicated that was wrong with apache configuration. To help troubleshoot I had turned on Debug=True in settings.py when I saw this error.
In the end I had to add a new directive to the static files configuration inside apache config. When the static files were not accessible and Debug in django settings was set to true this error was getting triggered somehow.
I got this error when trying to reverse (and reverse_lazy) using RedirectView and parameters from the url. The offending code looked like this:
from django.views.generic import RedirectView
from django.core.urlresolvers import reverse
url(r'^(?P<location_id>\d+)/$', RedirectView.as_view(url=reverse('dailyreport_location', args=['%(location_id)s', ]))),
The fix is to use this url in urlpatterns:
from django.views.generic import RedirectView
url(r'^(?P<location_id>\d+)/$', RedirectView.as_view(url='/statistics/dailyreport/%(location_id)s/')),
ANSWER: The fix so you can still use the name of the url pattern:
from django.core.urlresolvers import reverse
from django.http import HttpResponseRedirect
urlpatterns = patterns('',
....
url(r'^(?P<location_id>\d+)/$', lambda x, location_id: HttpResponseRedirect(reverse('dailyreport_location', args=[location_id])), name='location_stats_redirect'),
....
)
In my case, it was because of tuple unpacking. I only had one url and the root cause for this ImproperlyConfigured error was
TypeError: 'URLPattern' object is not iterable
I used a trailing comma at the end and it resolved the issue.
urlpatterns = (url(...) , )
Check the name of the variable.
The cause of my error was using "urlspatterns" in lieu of "urlpatterns".
Correcting the name of the variable solved the issue for me.
In my case I had the following error:
ImproperlyConfigured: The included URLconf 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.
The url patterns were valid, but the problem was an Import error caused by a typo. I typed restframework instead of rest_framework.
Note: For some reason, for me this error also went away after I saved another file. So the first time the error appeared, I had saved a file in which I specified the wrong widget in the forms.py file:
extra_field = forms.CharField(widget=forms.TextField())
instead of
extra_field = forms.CharField(widget=forms.TextInput())
After changing it to the correct version (TextInput) and saving the forms.py file, the error was still showing in my console. After saving another file (e.g. models.py) the error disappeared.
Check the imported modules in views.py if there is any uninstalled modules you found remove the module from your views.py file.
It's Fix for me
django.urls.reverse()-->django.urls.reverse_lazy()
This will instantly solve it.
Related
I'm following a django tutorial that is a little outdated and in the urls.py file of the first app's directory we need to configure where to direct django to for any url starting with 'notes/'.
There are two separate 'apps' inside the project. I'm in the first one, not notes.
This is the code currently. I added include to import statement:
from django.contrib import admin
from django.urls import path, include
urlpatterns = [
path('admin/', admin.site.urls),
path(url(r’^notes/‘, include('notes.urls'))),
]
Inside the urlpatterns object, on the first line, path('admin/', admin.site.urls), comes predefined, but I need to add a redirect such that django goes to a different app, called 'notes' and searches there for its entry point, since all of the urls will begin with ‘notes/’.
The tutorial says to use a regular expression and uses this code:
url(r’^notes/‘, include(notes.urls))
so that any url that starts with 'notes/' should be redirected to this other file notes.urls.
However the predefined ones that currently come out of the box with a django project start with path.
I enclosed my notes/n redirect line in path, but not sure if this is correct. Should I instead directly write:
url(r’^notes/‘, include(notes.urls))
Also, do I need to delete the first line provided?
path('admin/', admin.site.urls),
The tutorial has:
urlpatterns = patterns('',
url(r’^notes/‘, include(notes.urls)),
)
and no admin urls line. It's from 2014 I believe.
Simply do:
path('notes/', include('notes.urls'))
I just started up with django.
Following this tutorial, I ended up with the following urls.py:
from django.contrib import admin
from django.urls import include, path
urlpatterns = [
path('polls/', include('polls.urls')),
path('admin/', admin.site.urls),
]
This links to polls.urls.py which has its own router.
The part I don't like is the string literal 'polls.urls' which is, well... a string literal.
I would like to somehow reference the file directly using some of python's power, and have AT LEAST my IDE protect me.
What happens if I want to move that polls.urls.py file, or rename it, or rename polls? Should I trust my IDE to catch a reference from a string literal?
This is almost like doing this monstrosity, and is very hard for me to accept as the best practice.
It just seems odd to me.
Is there a way to use something less prone to errors than string literals in django's routers?
I don't see problems with using string literals as URLconf is loaded prior to starting of server (runserver)
If there is a problem you would get ModuleNotFound error
From source of include() :
if isinstance(urlconf_module, six.string_types):
urlconf_module = import_module(urlconf_module)
You would see good amount of import_module usage through Django framework and string literals.
Your IDE would know if there is unresolved reference (Pycharm does)
So from what I understand or what I know there are two ways of url mapping using the
path' orurl`
for path method you bound to do what you did that is:
from django.urls import path, include
but you are to also import your views.py
For second method your are to:
from django.conf.urls import url
then your are to import your views there as:
from .views import home_page
home_page being a function or class in your views.py
Example of mapping
url(r'^home/$', home_page),
so no need to actually to actually create urls.py if you use this method
I recently upgraded from an ancient version of Django to 1.6.8, and now I am getting the error described above inconsistently (more on that below).
I've seen several threads here that discuss this error, and the culprit usually seems to be a view that calls reverse() somewhere. However, nothing in any of my views does this.
In fact, I can trim my site's urls.py to just the admin site...
from django.conf.urls import patterns, include, url
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('',
# Examples:
# url(r'^$', 'Site-main.views.home', name='home'),
# url(r'^Site-main/', include('Site-main.foo.urls')),
# Uncomment the admin/doc line below to enable admin documentation:
# url(r'^admin/doc/', include('django.contrib.admindocs.urls')),
# This has to go before the admin site
url(r'^a/', include(admin.site.urls)),
)
...and I still get the error periodically.
"Periodically?"
Well, that's the other really weird thing here. Around half the time the page loads as expected, with no errors. The rest of the time I get the error.
Another odd thing: if I use the manage.py server instead of apache+wsgi, the error seems to go away, but I've had the admins completely re-start apache with no change, so I don't think it's just an old and confused httpd process causing the problem.
Does anyone have ideas?
I'm having this weird problem.
When I did this:
from django.core.urlresolvers import reverse
reverse('account-reco-about-you')
# returns '/accounts/recommendations/about-you/'
But when I did this:
# Doesn't Work
recommendations = login_required(RedirectView.as_view(url=reverse('account-reco-about-you')))
# Work
recommendations = login_required(RedirectView.as_view(url='/accounts/recommendations/about-you'))
Error message I get if unrelated. It says my last view is not found, which is there. Any explanation? Meantime, i'll make do with the non-reverse style.
This problem is to do with trying to reverse something at import time before the URLs are ready to be reversed. This is not a problem with RedirectView itself - it would happen with anything where you tried to reverse in your urls.py file, or possibly in a file imported by it.
In the development version of Django, there is a function called reverse_lazy specifically to help in this situation.
If you're using an earlier version of Django, there is a solution here: Reverse Django generic view, post_save_redirect; error 'included urlconf doesnt have any patterns'.
You need to use "reverse_lazy" that is defined in "django.core.urlresolvers" in Django 1.4 and above.
Here is an example urls.py:
from django.conf.urls import patterns, include, url
from django.views.generic import RedirectView
from django.core.urlresolvers import reverse_lazy
# Uncomment the next two lines to enable the admin:
from django.contrib import admin
admin.autodiscover()
urlpatterns = patterns('apps.website.views',
url(r'^$', 'home', name='website_home'),
url(r'^redirect-home/$', RedirectView.as_view(url=reverse_lazy('website_home')),
name='redirect_home'),
)
So in the above example, the url "/redirect-home" will redirect to "/". Hope this helps.
no need for reverse() or reverse_lazy().
simply specify the pattern_name parameter:
RedirectView.as_view(pattern_name='account-reco-about-you')
#wtower
pattern_name will be ok, but you may need to add namespace as below.
RedirectView.as_view(pattern_name='polls:index')
I'm following the Django tutorial and got stuck with an error at part 4 of the tutorial. I got to the part where I'm writing the vote view, which uses reverse to redirect to another view. For some reason, reverse fails with the following exception:
import() argument 1 must be string, not instancemethod
Currently my project's urls.py looks like this:
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.root)),
)
and the app urls.py is:
from django.conf.urls.defaults import *
urlpatterns = patterns('mysite.polls.views',
(r'^$', 'index'),
(r'^(?P<poll_id>\d+)/$', 'details'),
(r'^(?P<poll_id>\d+)/results/$', 'results'),
(r'^(?P<poll_id>\d+)/vote/$', 'vote'),
)
And the vote view is: (I've simplified it to have only the row with the error)
def vote(request, poll_id):
return HttpResponseRedirect(reverse('mysite.polls.views.results', args=(1,)))
When I remove the admin urls include from the project's urls.py, i.e. making it into:
urlpatterns = patterns('',
(r'^polls/', include('mysite.polls.urls')),
#(r'^admin/(.*)', include(admin.site.root)),
)
it works.
I've tried so many things and can't understand what I'm doing wrong.
The way you include the admin URLs has changed a few times over the last couple of versions. It's likely that you are using the wrong instructions for the version of Django you have installed.
If you are using the current trunk - ie not an official release - then the documentation at http://docs.djangoproject.com/en/dev/ is correct.
However, if you are using 1.0.2 then you should follow the link at the top of the page to http://docs.djangoproject.com/en/1.0/.