[Fixed]-Django login user, after successful login user stay on same page

1👍

You need to pass next URL in template and in login view check for next url.

In template:

<a href="{% url 'login' %}?next={{request.path}}">Login</a>

And in login view:

from django.utils.http import is_safe_url

def login(request):
    redirect_to = request.POST.get('next', request.GET.get('next', ''))
    # check form validity
    # authenticate user
    if redirect_to and is_safe_url(url=redirect_to, host=request.get_host()):
        return redirect(redirect_to)
    else:
        return redirect('index')

Leave a comment