Django Error: Your URL pattern is invalid. Ensure that urlpatterns is a list of url() instances
Asked Answered
B

2

5

After upgrading to Django 1.10, I get the following error when I run python manage.py runserver:

?: (urls.E004) Your URL pattern ('^$', 'myapp.views.home') is invalid. Ensure that urlpatterns is a list of url() instances.
HINT: Try using url() instead of a tuple.

My urlpatterns are as follows:

from myapp.views import home

urlpatterns = [
    (r'^$', home, name='home'),
]
Burnell answered 5/8, 2016 at 9:59 Comment(0)
B
14

To simplify URL configs, patterns() was deprecated in Django 1.8, and removed in 1.10 (release notes). In Django 1.10, urlpatterns must be a list of url() instances. Using a tuple in patterns() is not supported any more, and the Django checks framework will raise an error.

Fixing this is easy, just convert any tuples

urlpatterns = [
    (r'^$', home, name='home'),  # tuple
]

to url() instances:

urlpatterns = [
    url(r'^$', home, name='home'),  # url instance
]

If you get the following NameError,

NameError: name 'url' is not defined

then add the following import to your urls.py:

from django.conf.urls import url

If you use strings in your url patterns, e.g. 'myapp.views.home', you'll have to update these to use a callable at the same time. See this answer for more info.

See the Django URL dispatcher docs for more information about urlpatterns.

Burnell answered 5/8, 2016 at 9:59 Comment(0)
P
0

Check to see if you have used URL patterns like this:

urlpatterns += (
    (r'^404/$', page_not_found_view),
)

Change it to:

urlpatterns += [
    url(r'^404/$', page_not_found_view),
]
Proverbial answered 25/8, 2017 at 4:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.