1
我遇到了一个奇怪的问题,虽然我现在已经找到了一个解决方案,但我认为了解导致错误的原因会很有帮助。我有一个Django项目的应用程序和网址如下:django中的网址排序
urlpatterns = patterns('',
url(r'^$', UserProfileListView.as_view(),
name='userprofile_list'),
url(r'^(?P<username>[\[email protected]+-_]+)/changepassword/$',
password_change, name='change_password'),
url(r'^(?P<username>[\[email protected]+-_]+)/$',
profile_detail,
name='userprofile_detail'),
)
当我将浏览器指向change_password一切工作正常。不过是我的URL命令如下:
urlpatterns = patterns('',
url(r'^$', UserProfileListView.as_view(),
name='userprofile_list'),
url(r'^(?P<username>[\[email protected]+-_]+)/$',
profile_detail,
name='userprofile_detail'),
url(r'^(?P<username>[\[email protected]+-_]+)/changepassword/$',
password_change, name='change_password'),
)
我得到一个错误,由于404页,未发现该视图获取的用户名=用户名/ changepassword而不是用户名的事实=用户名
会是什么是否以这种方式解释网址的原因,以及为什么它在第一种情况下工作?
因为第一正则表达式捕获两者。这就是订单很重要的原因。 –