简体   繁体   English

AttributeError:'NoneType'对象没有属性'is_active'

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

I'm trying to set up a front-end user authentication check with Parsley.js and Django. 我正在尝试使用Parsley.js和Django设置前端用户身份验证检查。
This is my view 这是我的看法

@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')

The validator 验证者

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/'
);

But when the "password_check" function is called I get this error: 但是,当调用“ password_check”函数时,出现此错误:
AttributeError at /password_check/ / password_check /处的AttributeError
'NoneType' object has no attribute 'is_active' 'NoneType'对象没有属性'is_active'

I've tried to put a print after "request.is_ajax()" to test the values, and it sometimes catches the e-mail and sometimes the password, but never the two together. 我试图在“ request.is_ajax()”之后放一个打印纸来测试这些值,它有时会捕获电子邮件,有时会捕获密码,但不会同时捕获两者。

How could I fix that? 我该如何解决?

When using authenticate function of django, user object is only returned when authentication is successful. 使用django的authenticate功能时,仅当验证成功后才返回用户对象。

When incorrect credentials are supplied, authenticate returns None . 如果提供了不正确的凭据,则authenticate返回None This is why you are getting AttributeError . 这就是为什么您得到AttributeError

To solve this, add an additional check for user, like this: 要解决此问题,请为用户添加额外的支票,如下所示:

if user and user.is_active:

This way, user's is_active attribute will only be checked if user exists. 这样,仅当用户存在时才检查用户的is_active属性。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何解决 AttributeError: 'NoneType' object has no attribute 'encode' in Django - How to solve AttributeError: 'NoneType' object has no attribute 'encode' in Django AttributeError: 'NoneType' object 通过 JS 发送表单时没有属性 'decode' - AttributeError: 'NoneType' object has no attribute 'decode' when sending a form through JS '错误:\'NoneType\' object 没有属性 \'startswith\' - 'ERROR: \'NoneType\' object has no attribute \'startswith\' AttributeError:'WebElement'对象没有属性'getElementById' - AttributeError: 'WebElement' object has no attribute 'getElementById' AttributeError:'str'对象没有属性'resolve' - AttributeError: 'str' object has no attribute 'resolve' sanic( ) - AttributeError: 'Request' object 没有属性 'split' - sanic( ) - AttributeError: 'Request' object has no attribute 'split' AttributeError问题:'WebDriver'对象没有属性'manage' - Issue with AttributeError: 'WebDriver' object has no attribute 'manage' Flask + Ajax 集成:AttributeError:'WSGIRequestHandler' 对象没有属性 'environ' - Flask + Ajax Integration : AttributeError: 'WSGIRequestHandler' object has no attribute 'environ' AttributeError:“ unicode”对象没有属性“ get”-在Django表单中 - AttributeError: 'unicode' object has no attribute 'get' - In Django Forms /Customer/Visit 'WSGIRequest' object 处的 AttributeError 在 django 中没有属性 'is_ajax' - AttributeError at /Customer/Visit 'WSGIRequest' object has no attribute 'is_ajax' in django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM