views:

227

answers:

1

A few of the options in the django settings file are urls, for example LOGIN_URL and LOGIN_REDIRECT_URL. Is it possible to avoid hardcoding these urls, and instead use reverse url mapping? At the moment this is really the only place where I find myself writing the same urls in multiple places.

+10  A: 

In urls.py, import settings:

from django.conf import settings

Then add the url pattern

urlpatterns=('',
    ...
    url('^%s$' %settings.LOGIN_URL[1:], 'django.contrib.auth.views.login', 
        name="login")
    ...
)

Note that you need to slice LOGIN_URL to remove the leading forward slash.

In the shell:

>>>from django.core.urlresolvers import reverse
>>>reverse('login')
'/accounts/login/'
Alasdair
Ah, good solution, I didn't consider going from settings -> urls, only the other way around. +1
TM