2016-11-08 3 views
1

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.

+0

Haben Sie debugged Ihre JS im Browser, um zu sehen, welche Antwort Sie tatsächlich erhalten? –

+0

Ja, es gibt den Status zurück: 0 was gut ist. –

+0

Was gibt diese Funktion authenticate() zurück? – FeedTheWeb

Antwort

3

Das Problem war wie immer sehr einfach. Ich entfernte versehentlich login(request,user) Zeile nach authenticate so Benutzer wurde authentifiziert, aber nicht angemeldet.

1

Ich denke, das Problem ist in Ihrem Js-Code, wo Sie Ihre aktuelle Seite neu laden. Ich glaube eher, dass Sie auf die Zielseite/Startseite zum Benutzer umleiten müssen, wenn der Benutzer erfolgreich authentifiziert wurde.

Eg.

$.ajax({ 
      url: '/ajax/login/', 
      type: 'post', 
      data: { 
       'password': password, 
       'username': username, 
      }, 
      success: function (data) { 
       if (data.status == 0) { 
        window.location = "http://yourhost/landing-url" 
       } else { 
        $('.login-modal-credentials-incorrect').show(); 
        return false; 
       } 
      } 
     }); 
3

Sie haben vergessen login() nach authenticate() zu nennen.

from django.contrib.auth import authenticate, login 

def ajax_login_(request): 
    ... 
    user = authenticate(username=username, password=password) 
    if user is not None: 
     login(request, user) 

Weitere Informationen finden Sie in der Dokumentation unter how to log a user in.

Verwandte Themen