Reputation: 768
I have encountered a strange issue and although I have found a solution for now I think it would be helpful to have an idea of what is causing the error. I have an app in a Django project and the urls are as follows:
urlpatterns = patterns('',
url(r'^$', UserProfileListView.as_view(),
name='userprofile_list'),
url(r'^(?P<username>[\w.@+-_]+)/changepassword/$',
password_change, name='change_password'),
url(r'^(?P<username>[\w.@+-_]+)/$',
profile_detail,
name='userprofile_detail'),
)
When i point the browser to change_password everything works fine. However is I have the urls ordered as follows:
urlpatterns = patterns('',
url(r'^$', UserProfileListView.as_view(),
name='userprofile_list'),
url(r'^(?P<username>[\w.@+-_]+)/$',
profile_detail,
name='userprofile_detail'),
url(r'^(?P<username>[\w.@+-_]+)/changepassword/$',
password_change, name='change_password'),
)
I get an error 404 page not found due to the fact that the view receives the username=username/changepassword and not username=username
What would be the cause of the url being interpreted in such a manner and why is it working in the first instance?
Upvotes: 1
Views: 167
Reputation: 77912
Dan Klasson's comment is the answer. Just to elaborate a bit, you could have found out easily by testing your regexp:
>>> import re
>>> re.match(r"^(?P<username>[\w.@+-_]+)/$", "foobar/changepassword/")
<_sre.SRE_Match object at 0x7f949c54be40>
FWIW the problem is with the \w
specifier which might not do exactly what you expect:
>>> re.match(r"\w", "foobar/changepassword/")
<_sre.SRE_Match object at 0x7f949c5a1d30>
Upvotes: 2