Apostolos
Apostolos

Reputation: 8101

urls.py and redirecting urls

I have the following urls.py file

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')),

and accounts.urls has two records

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

But when I access localhost:8000/crm/ (with or without the trailing slash) it takes me to localhost:8000/crm/accounts/ and not accounts/login as it says in RedirectView. I was testing something with using /crm/accounts as a parameter to RedirectView.as_view but after changint it it won't work. Restarted server and browser.

Upvotes: 1

Views: 73

Answers (1)

falsetru
falsetru

Reputation: 368894

Change order of following two lines. (also prepend ^ in front of crm/.)

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

Or, change crm/ to ^crm/$ to make it only match crm/, not /crm/accounts/....

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

Othersie, access to /crm/accounts/login , /crm/accounts/logout is handled by RedirectView.as_view(url='/crm/accounts/login') because the first url pattern matched first is used.

Upvotes: 2

Related Questions