[Fixed]-How to set settings.LOGIN_URL to a view function name in Django 1.5+

22👍

Django computes this url in django.contrib.auth.views:redirect_to_login function as:

resolved_url = resolve_url(login_url or settings.LOGIN_URL)

Therefore you should set it as string:

LOGIN_URL = 'my_app.views.sign_in'

Also in settings.py you can use reverse_lazy function:

from django.core.urlresolvers import reverse_lazy
LOGIN_URL = reverse_lazy('my_app.views.sign_in')

https://docs.djangoproject.com/en/1.5/ref/urlresolvers/#reverse-lazy

👤kubus

5👍

If you do not want to link the LOGIN_URL to the “view” (you can change on other), then you can link to the named URL in settings.py:

from django.core.urlresolvers import reverse_lazy

LOGIN_URL = reverse_lazy('login')

where “login” is something like:

url(r'^accounts/login/$', 'my_app.view.Login', name='login'),

then, if you change the view on the other, you don’t need to make changes in settings.py

5👍

Assuming you’ve set the path name in urls.py, you can use 'application_name:view_name' as the LOGIN_URL value in settings.py, like so:

application/urls.py

...
path('login/', views.login, name='login'),
...

project/settings.py

LOGIN_URL = 'application:login'

https://docs.djangoproject.com/en/2.1/ref/settings/#login-url

👤kas

2👍

Django 2.1.5

django.urls import reverse_lazy
LOGIN_URL = reverse_lazy('namespace of any url used in your project ')

1👍

If you have the view in the project root ( not inside an app ) you just specify the view name in settings.py:

LOGIN_URL = 'login'

Leave a comment