0

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

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

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

4

1 回答 1

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..
于 2012-07-26T22:58:25.013 回答