0
votes

I have this code for a custom login:

def login(request):
    if request.user.is_authenticated:
        return render(request, 'listings/index.html', {})

    if request.method == 'POST':
        username = request.POST.get('username')
        password = request.POST.get('password')
        user = authenticate(request, username=username, password=password)
        if user is not None:
            if not request.POST.get('remember_me', None):
                request.session.set_expiry(0)
            auth_login(request, user)
            return redirect('dashboard')
        else:
            messages.error(request, "Les informations que vous venez d'entrer sont incorrects. Veuillez réessayer.")
            return render(request, 'dashboard/login.html', {})
    return render(request, 'dashboard/login.html', {'randomtip': random.choice(RandomTip.objects.all())})

And all my views have @login_required and @user_passes_test decorators. As you can see in the login function, when the user is authenticated and logged in, s/he gets redirected to dashboard. But I have a session expiry limit (where users have their sessions expired after 6 minutes). And when they are in a specific page, they get redirected to the login after the expiry of the cookie. The problem here is that if they are in a page other than dashboard, after they login they get redirected to the dashboard.

I want them to stay in the same page after they login again. How can I know the path of the current page visited and put it here: return redirect(current_page)?

1

1 Answers

0
votes

this is done automatically by django when the cookie expires the user won't pass the @login_required he will be passed to the login page with html attributes like this

http://127.0.0.1:8000/login/?next=/the_page_he_was_in/

the key here is /?next=/ in the url it will redirect the normal login redirect and send the user direct to the page he was in