Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django: Dynamic LOGIN_URL variable

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?

like image 484
Deniz Dogan Avatar asked Jul 06 '09 19:07

Deniz Dogan


2 Answers

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
like image 195
AlbertoPL Avatar answered Nov 08 '22 00:11

AlbertoPL


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 '$'

like image 26
joej Avatar answered Nov 08 '22 01:11

joej