2017-06-20 72 views
1

是否有可能在用戶通過AuthenticationFormconfirm_login_allowed後重定向。重定向自定義AuthenticationForm一旦通過身份驗證 - Django

例如,我試圖做

class LoginForm(AuthenticationForm): 

    def confirm_login_allowed(self, user): 
     if not user.is_active: 
      raise forms.ValidationError('There was a problem with your login.', code='invalid_login') 
     elif user.is_staff and not self.has_2fa(): 
      logger.info('is staff but does not have 2FA, redirecting to Authy account creator') 
      return redirect('admin/authy_me/authenticatormodel/') 
     elif user.is_staff and self.has_2fa(): 
      logger.info("is staff and 2FA enabled") 
     elif not user.is_staff and not self.has_2fa(): 
      logger.info('is not staff and does not have 2FA') 
只爲驗證錯誤使用

confirm_login_allowed在形式將用戶重定向?如果有,還有其他方法嗎?

回答

0

得到了答案。 confirm_login_allowed不能有任何重定向,你將不得不創建一個自定義視圖,然後在那裏做重定向。

的例子views.py

def login(request): 
    if request.user.is_staff and not has_2fa(request): 
     logger.info('is staff but does not have 2FA, redirecting to Authy account creator') 
     return redirect('admin/authy_me/authenticatormodel/') 
    elif request.user.is_staff and has_2fa(request): 
     logger.info("is staff and 2FA enabled") 
    elif not request.user.is_staff and not has_2fa(request): 
     logger.info('is not staff and does not have 2FA') 

    defaults = { 
     'authentication_form': LoginForm, 
     'template_name': 'core/login.html', 
    } 

    return auth_login(request, **defaults) 

urls.py,而不是進口from django.contrib.auth.views import login進口from app_name.views import login再加入

url(r'^login/?$', app_name.login, name="login")