urls.py和重定向网址

时间:2014-07-15 13:53:26

标签: python django django-urls

我有以下urls.py文件

urlpatterns = patterns('',

    url(r'^$', include('main.urls')),
    url(r'crm/',RedirectView.as_view(url='/crm/accounts/login'), name='home'),
    url(r'^crm/accounts/', include('accounts.urls')),

和accounts.urls有两条记录

urlpatterns = patterns('',
    url(r'^login/', login, name='login'),
    url(r'^logout/', logout, name='logout'),
)

但是当我访问localhost:8000 / crm /(有或没有尾部斜杠)时,它会将我带到localhost:8000 / crm / accounts /而不是像RedirectView中所述的accounts / login。我正在使用/ crm / accounts作为RedirectView.as_view的参数进行测试,但是在改变后它将无法正常工作。重启服务器和浏览器。

1 个答案:

答案 0 :(得分:2)

更改以下两行的顺序。 (还在^前面加crm/。)

url(r'^crm/',RedirectView.as_view(url='/crm/accounts/login'), name='home'),
url(r'^crm/accounts/', include('accounts.urls')),

或者,将crm/更改为^crm/$,使其仅与crm/匹配,而不是/crm/accounts/...

url(r'^crm/$',RedirectView.as_view(url='/crm/accounts/login'), name='home'),
url(r'^crm/accounts/', include('accounts.urls')),

其他人,访问/crm/accounts/login/crm/accounts/logoutRedirectView.as_view(url='/crm/accounts/login')处理,因为首先使用匹配的第一个网址模式。