6

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?

2 Answers 2

7

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
Sign up to request clarification or add additional context in comments.

2 Comments

But how do I fit that into urlpatterns? :/
from django.conf.urls.defaults import * from projectname.settings import LOGIN_URL urlpatterns = patterns('', (r'^articles/2003/' + LOGIN_URL + '/$', 'news.views.special_case_2003'),
4

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.