2017-06-20 66 views
0

我有一個應用程序authy_me並寫了一個定製URLrequest.user總是返回AnonymousUser - Django的

url(r'^login/$', authy_views.log_me_in, name="login") 

log_me_in觀點有以下幾點:

from django.contrib.auth.forms import AuthenticationForm 
from django.contrib.auth.views import login as auth_login 
def log_me_in(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 redirecting to Authy verification") 
     return redirect('2fa') 
    elif not request.user.is_staff and not has_2fa(request): 
     logger.info('is not staff and does not have 2FA') 

    defaults = { 
     'authentication_form': AuthenticationForm, 
     'template_name': 'login.html', 
    } 
    return auth_login(request, **defaults) 

當我打印request.user我把它作爲AnonymousUser但事情是,它工作,我沒有改變任何東西!我不確定問題是什麼。

幫助!!

回答

1

你需要傳遞請求auth_login()之前進行身份驗證的用戶,

from django.contrib.auth import authenticate 

user = authenticate(username=username, password=password) 

檢查,如果用戶是有效用戶,

if not user: 
    return False 

現在,你可以通過你的請求auth_login()

希望這會有所幫助

+0

謝謝。那工作。 – Akshay