Answer the question
In order to leave comments, you need to log in
Django redirect after login?
Hello.
I want to make sure that after authorization the user is automatically redirected to the page from which he went to the authorization page.
Did the deauthorization in the same way. If the user is in some section of the site and clicks on the exit, then he remains in this section.
Here is the code from the view.
Authorization - redirect to the main page:
def login(reguest):
args = {}
args.update(csrf(reguest))
if reguest.POST:
username = reguest.POST.get('username', '')
password = reguest.POST.get('password', '')
user = auth.authenticate(username=username, password=password)
if user is not None:
auth.login(reguest, user)
return redirect('/')
else:
args['login_error'] = 'Пользователь не найден'
return render_to_response('login.html', args)
else:
return render_to_response('login.html', args)
def logout(reguest):
return_path = reguest.META.get('HTTP_REFERER','/') # При выходе с аккаунта остаемся на странице с которой вышли!
auth.logout(reguest)
return redirect(return_path)
if user is not None:
return_path = reguest.META.get('HTTP_REFERER','/')
auth.login(reguest, user)
return redirect(return_path)
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question