B
B
blazer052015-12-30 13:47:24
Django
blazer05, 2015-12-30 13:47:24

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)

Deauthorization - exit:
def logout(reguest):
    return_path = reguest.META.get('HTTP_REFERER','/') # При выходе с аккаунта остаемся на странице с которой вышли!
    auth.logout(reguest)
    return redirect(return_path)

With the exit applied return_path = reguest.META.get('HTTP_REFERER','/') and returned the redirect(return_path). Similarly tried to make and authorization, but it did not turn out.
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

1 answer(s)
A
Andrey K, 2015-12-30
@mututunus

Django can do this out of the box https://github.com/django/django/blob/master/djang...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question