登录错误后Django重定向

时间:2012-07-26 22:01:04

标签: python django authentication

我是Django的新手,我知道要在登录后重定向我必须设置参数'page'。但这仅在登录成功时有效。

当发生一些错误时,我怎么能做同样的事情?

Ps:我目前也使用django-registration和简单的后端

1 个答案:

答案 0 :(得分:1)

我认为这就是你要找的东西:

# 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..