I'm trying to set up a front-end user authentication check with Parsley.js and 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:
AttributeError at /password_check/
'NoneType' object has no attribute '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.
How could I fix that?
When using authenticate
function of django, user object is only returned when authentication is successful.
When incorrect credentials are supplied, authenticate
returns None
. This is why you are getting 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.