Question

Currently, in my settings module I have this:

LOGIN_URL = '/login'

If I ever decide to change the login URL in urls.py, I'll have to change it here as well. Is there any more dynamic way of doing this?

Was it helpful?

Solution

Settings IS where you are setting your dynamic login url. Make sure to import LOGIN_URL from settings.py in your urls.py and use that instead.

from projectname.settings import LOGIN_URL

OTHER TIPS

This works for me ... with LOGIN_URL = '/accounts/login'

If the problem is that settings.py has ...

LOGIN_URL = '/login/'  # <-- remember trailing slash!

... but, urls.py wants ...

url(r'^login/$', 
      auth_views.login, {'template_name': '/foo.html'}, 
            name='auth_login'),

Then do this:

# - up top in the urls.py
from django.conf import settings

# - down below, in the list of URLs ...
# - blindly remove the leading '/' & trust that you have a trailing '/'
url(r'^%s$' % settings.LOGIN_URL[1:], 
      auth_views.login, {'template_name': '/foo.html'}, 
            name='auth_login'),

If you can't trust whomever edits your settings.py ... then check LOGIN_URL startswith a slash & snip it off, or not. ... and then check for trailing slash LOGIN_URL endswith a slash & tack it on, or not ... and and then tack on the '$'

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top