The Django-AttributeError 'User' object does not have a 'backend' attribute (but ... does it?)

To sign users after registration, I manually set the user.backend property. This usually works in my opinion. In this case, I am trying to register a user through AJAX. It raises 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 AJAX. THIS VIEW THIS 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(); }); 

To a large extent, this exact code works for me non-ajax. What gives?

thank

+47
django django-models user django-authentication
May 17 '11 at 17:32
source share
2 answers

You must call authenticate to call login . authenticate sets an object attribute, noting that the backend successfully verified it and cleared it for login, which does not happen in your code (and that this attribute is missing).

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

+77
May 18 '11 at 4:12
source share

I will post this as an answer, but I have to https://stackoverflow.com/users/558699/ben in the comments above and https://stackoverflow.com/users/questions/114116/ .... I looked through this question and skipped what I was looking for in the comments. Adding a backend manually was a (hacked) fix for me twice:

 user.backend = 'django.contrib.auth.backends.ModelBackend' login(request, user) 

In both cases, I rely on other authentication methods (email confirmation and administrator authentication session) to verify that the login is allowed as that user.

+38
May 21 '14 at
source share



All Articles