M
M
Meekey2018-04-28 21:14:31
Django
Meekey, 2018-04-28 21:14:31

How to automatically go to the login page?

How to make it so that if the user is not authorized, then he is redirected from any page to the login page. LOGIN_URL not working(
urls.py setting.py
path('login/', auth_views.login, name='login')
LOGIN_URL = '/login/'

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir, 2018-04-29
@vintello

LOGIN_URL works, you just need to read the documentation to the end

from django.conf import settings
from django.shortcuts import redirect

def my_view(request):
    if not request.user.is_authenticated():
        return redirect('%s?next=%s' % (settings.LOGIN_URL, request.path))

J
JonGalt, 2018-05-03
@JonGalt

Use the login_required() decorator in a view
https://djbook.ru/rel1.8/topics/auth/default.html#...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question