Search code examples
djangoauthenticationdjango-modelsdjango-authentication

Django-AttributeError 'User' object has no attribute 'backend' (But....it does?)


In order to sign users in after registering them, I manually set the user.backend property. It normally works in my views. In this instance, I'm trying to register the user via AJAX. It is raising an AttributeError.

Here is my code:

 def register_async(request):
    if request.method=='POST':

    userform=MyUserCreationForm(request.POST)
    if userform.is_valid():
        #username of <30 char is required by Django User model.  I'm storing username as a hash of user email 

        user=userform.save(commit=False)
        user.username=hash(user.email)
        user.backend='django.contrib.auth.backends.ModelBackend'
        user.save()


        auth.login(request,user)
        user_status=1
        user_fname=user.first_name
        user_data=[{'user_status':user_status, 'user_fname':user_fname}]
        json_data=json.dumps(user_data)
        response=HttpResponse()
        response['Content-Type']="text/javascript"
        response.write(json_data)
        return response 

    else:
        user_data=[{'user_status':"0"}]
        json_data=json.dumps(user_data)
        response=HttpResponse()
        response['Content-Type']="text/javascript"
        response.write(json_data)
        return response 
else:
    return HttpResponse()

EDIT-- HERE'S THE AJAX. IT SEEMS PRETTY STANDARD

     //ajax registration.  
$('input#register_submit').click(function(event){
    $(this).attr('disabled','disabled');
    $('<div class="register-animation"><img src="{{site}}media/ajax-loader3.gif"/></div>').appendTo('#register_modal_btn');

    $.post("/register/", $('div#register_side form').serialize(), 
        function(data){
            $.each(data,function(){
            if(this.user_status==1){
                $('.register-animation').remove();
                $('.right_section .top').html('<ul><li class="sep_nav">Hi, '+ this.user_fname + '</li><li class="sep+nav"><a href="http://nabshack.com/logout/">Log Out</a></li><li class="refar_friend"><a href="http://nabshack.com/referral/">Refer a friend and get $50</a></li></ul>');
                $('#post_login_modal').dialog("close");

                $('a.login').unbind('click');
                $('li a.account').unbind('click');

            }       
            else{
            $('input#register_submit').removeAttr('disabled');
            $('.register-animation').remove();
            window.location='{{site}}register';
            }

        });
    },'json');
    return false;
    event.stopPropagation();
});

Pretty much this exact code works in non-ajax views for me. What gives?

Thanks


Solution

  • You must call authenticate before you can call login. authenticate sets an attribute on the object noting which backend has successfully validated it and clearing it for login, which isn't happening in your code (and that's the attribute that is missing).

    Documentation: https://docs.djangoproject.com/en/1.8/topics/auth/default/#how-to-log-a-user-in -- check out the little callout that says "calling authenticate() first".