ajax - Django acts like user is logged in but isn't -


i have ajax login in django project. ajax_login view gets post, authenticate user , returns jsonresponse. seems work correct user isn't logged in.

the weird in view, i'm testing whether user logged in , are.

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 being printed console user 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;                     }                 }             });         }     }) }); 

can't figure out problem. stopped working.

you have forgotten call login() after authenticate().

from django.contrib.auth import authenticate, login  def ajax_login_(request):     ...     user = authenticate(username=username, password=password)     if user not none:         login(request, user) 

see docs on how log user in more info.


Comments

Popular posts from this blog

php - How to add and update images or image url in Volusion using Volusion API -

javascript - jQuery UI Splitter/Resizable for unlimited amount of columns -

javascript - IE9 error '$'is not defined -