簡體   English   中英

AttributeError:'NoneType'對象沒有屬性'is_active'

[英]AttributeError: 'NoneType' object has no attribute 'is_active'

我正在嘗試使用Parsley.js和Django設置前端用戶身份驗證檢查。
這是我的看法

@requires_csrf_token
def password_check(request):
    email = request.POST.get('email')
    password = request.POST.get('password1')
    user = authenticate(email=email, password=password)

    if request.is_ajax():
        if user.is_active:
            res = "These password and e-mail are ok."
            ajax_vars_login = {'response': res, 'email': email, 'password': password}
            json_data_login = json.dumps(ajax_vars_login)
        else:
            res = "The e-mail or the password field aren't correct."
            ajax_vars_login = {'response': res, 'email': email, 'password': password}
            json_data_login = json.dumps(ajax_vars_login)

        return HttpResponse(json_data_login, content_type='application/json')

驗證者

Parsley.addAsyncValidator(
  'emailPwCombination', function (xhr) {
       var password = $('#password').parsley();
       var email = $('#email').parsley();
       var response = xhr.responseText;
       var jsonResponse = JSON.parse(response);
       var jsonResponseText = jsonResponse["response"];

       if(jsonResponseText == 'The password and e-mail are ok.')
           return true;
       if(jsonResponseText == '404')
           return false;
  }, '/password_check/'
);

但是,當調用“ password_check”函數時,出現此錯誤:
/ password_check /處的AttributeError
'NoneType'對象沒有屬性'is_active'

我試圖在“ request.is_ajax()”之后放一個打印紙來測試這些值,它有時會捕獲電子郵件,有時會捕獲密碼,但不會同時捕獲兩者。

我該如何解決?

使用django的authenticate功能時,僅當驗證成功后才返回用戶對象。

如果提供了不正確的憑據,則authenticate返回None 這就是為什么您得到AttributeError

要解決此問題,請為用戶添加額外的支票,如下所示:

if user and user.is_active:

這樣,僅當用戶存在時才檢查用戶的is_active屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM