[Answered ]-Maintaining current URL after incorrect password entry β€” Django

1πŸ‘

βœ…

In this case, you have to check to see if the user is authenticated successfully, and handle if not. Try something like this:

def login_page(request):
    form = LoginForm(request.POST or None)
    context = {"form": form}
    next_ = request.GET.get('next')
    next_post = request.POST.get('next')
    redirect_path = next_ or next_post or None
    if form.is_valid():
        username = form.cleaned_data.get("username")
        password = form.cleaned_data.get("password")
        user = authenticate(request, username=username, password=password)
        if user is not None:
            login(request, user)
            messages.success(request, f'You are now logged in.')
            if is_safe_url(redirect_path, request.get_host()):
                return redirect(redirect_path)
            else: 
                return redirect("/")
        else:
            messages.warning(request, f'Login error. The email address and/or password is incorrect.')
    return render(request, "accounts/login.html", context)

πŸ‘€Milo Persic

Leave a comment