Django-AttributeError'User'对象没有属性'后端'(但是....它是吗?) [英] Django-AttributeError 'User' object has no attribute 'backend' (But....it does?)

查看:721
本文介绍了Django-AttributeError'User'对象没有属性'后端'(但是....它是吗?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了在注册用户后登录,我手动设置user.backend属性。它通常在我的看法。在这种情况下,我想通过AJAX注册用户。正在提高AttributeError。

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.

这是我的代码:

 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()

编辑 - 这是AJAX。 IT SEEMS PRETTY STANDARD

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();
});

几乎这个确切的代码对我来说是非ajax视图。什么给了?

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

谢谢

推荐答案

你必须调用 authenticate ,然后才能调用 login authenticate 在对象上设置一个属性,注意哪个后端已经成功验证了它,并清除它进行登录,这在代码中没有发生(这是缺少的属性) 。

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).

文档: https://docs.djangoproject.com/en/1.8/topics/auth/default/#how-to-log-a-user-in - 查看呼叫 authenticate()的小标注。

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".

这篇关于Django-AttributeError'User'对象没有属性'后端'(但是....它是吗?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆