[英]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.