简体   繁体   English

登录错误后Django重定向

[英]Django Redirect after Login Error

Im new to Django and I know that to redirect after login I have to set the parameter 'page'. 我是Django的新手,我知道登录后要重定向,我必须设置参数'page'。 But this only works when the login is successful. 但这仅在登录成功时有效。

How can i do the same thing when some error occurs?? 发生某些错误时,我该怎么做?

Ps: Im currently also using django-registration with simple backend Ps:我目前还使用带有简单后端的django-registration

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM