Ich habe eine AJAX
Anmeldung in meinem Django-Projekt. Der Ajax_login view
ruft POST auf, authentifiziert Benutzer und gibt JsonResponse
zurück. Alles scheint korrekt zu funktionieren, aber der Benutzer ist nicht wirklich angemeldet.Django verhält sich wie Benutzer ist eingeloggt, aber ist nicht
Das Seltsame ist, dass in der Ansicht, ich teste, ob der Benutzer angemeldet ist und sie sind.
VIEW
@csrf_exempt
def ajax_login_(request):
username = request.POST.get('username')
password = request.POST.get('password')
user = authenticate(username=username,password=password)
if user:
if user.is_authenticated():
print 'IS LOGGED IN' #THIS IS BEING PRINTED INTO CONSOLE SO USER IS LOGGED IN
return JsonResponse({'status':0})
return JsonResponse({'status':1})
AJAX
$(document).ready(function() {
$('.login-form').find('.submitButton').click(function (e) {
var next = $(this).closest('.login-form').find('input[name="next"]').val();
var password = $(this).closest('.login-form').find('#id_password').val();
var username =$(this).closest('.login-form').find('#id_username').val();
e.preventDefault();
var errors = 0;
if (username == '') {
$('#id_username_required').show();
errors += 1;
} else {
$('#id_username_required').hide();
}
if (password == '') {
$('#id_password_required').show();
errors += 1;
} else {
$('#id_password_required').hide();
}
if (errors == 0) {
$.ajax({
url: '/ajax/login/',
type: 'post',
data: {
'password': password,
'username': username,
},
success: function (data) {
if (data.status == 0) {
window.location.reload();
} else {
$('.login-modal-credentials-incorrect').show();
return false;
}
}
});
}
})
});
Kann nicht heraus, wo das Problem ist. Es hörte plötzlich auf zu arbeiten.
Haben Sie debugged Ihre JS im Browser, um zu sehen, welche Antwort Sie tatsächlich erhalten? –
Ja, es gibt den Status zurück: 0 was gut ist. –
Was gibt diese Funktion authenticate() zurück? – FeedTheWeb