2012-07-26 139 views
0

Im新的Django,我知道要登录后重定向,我必须设置参数'页面'。但是这只有在登录成功时才有效。

当发生某些错误时,我该如何做同样的事情?

PS:即时通讯目前也使用Django的注册用简单的后端Django重定向后登录错误

回答

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

您需要'从django.contrib.auth进口authenticate' – nlassaux 2012-07-26 23:02:33

+0

很好的答案,谢谢!但我认为问题在于django注册,如果登录失败,它会自动调用帐户/登录,并且它具有名为'registration/login.html'(使用简单后端)的默认模板。我希望重定向到其他视图或者以其他模板的形式调用。 – nsbm 2012-07-27 00:34:48

+0

你可以添加你的设置:'LOGIN_URL ='/ login /''也许它会解决你的问题。 **编辑:**或另一个网址^^ – nlassaux 2012-07-27 00:36:25