登录错误后的Django重定向 [英] Django Redirect after Login Error

查看:55
本文介绍了登录错误后的Django重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

出现某些错误时,我该怎么做?

附:我目前还使用带有简单后端的django-registration

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 error occurs??

Ps: Im currently also using django-registration with simple backend

推荐答案

我认为这就是您要寻找的:

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

这篇关于登录错误后的Django重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆