2012-07-26 138 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