Skip to content Skip to sidebar Skip to footer

Django Redirect After Login Error

Im new to Django and I know that to redirect after login I have to set the parameter 'page'. But this only works when the login is successful. How can i do the same thing when some

Solution 1:

I think it's what you are looking for:

# Login
def connection(request):

    # Redirect to dashboard if the user is log
    if request.user.is_authenticated():
        return redirect('YourProject.views.home')

    # Control if a POST request has been sent.
    if request.method == 'POST':
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)

        if user is not None: #Verify form's content existence
            if user.is_active: #Verify validity
                login(request, user)
                return redirect('/index') #It's ok, so go to index
            else:
                return redirect('/an_url/') #call the login view

    return render(request, 'login.html', locals())  #You can remove local() it is for user's data viewing..

Post a Comment for "Django Redirect After Login Error"