How to set settings.LOGIN_URL for a view function name in Django 1.5+

As in Django 1.5, you can set LOGIN_URL the name of the view function, but I could not figure out how to specify this correctly.

LOGIN_URL = my_app.views.sign_in 

... does not work. I get an error,

 ImproperlyConfigured: The SECRET_KEY setting must not be empty. 
+7
source share
4 answers

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

 resolved_url = resolve_url(login_url or settings.LOGIN_URL) 

Therefore, you should set it as a string:

 LOGIN_URL = 'my_app.views.sign_in' 

Also in settings.py you can use the 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

+14
source

If you do not want to associate LOGIN_URL with the "view" (you can change it to another), then you can link to the specified URL in settings.py :

 from django.core.urlresolvers import reverse_lazy LOGIN_URL = reverse_lazy('login') 

where the "login" is something like:

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

then, if you change the view to another, you do not need to make changes to settings.py

+3
source

Assuming you set the path name to 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

+1
source

Django 2.1.5

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

Source: https://habr.com/ru/post/952425/


All Articles