AnonymousUser'对象没有'backend'属性
我在使用django-socialregistration的时候,遇到了以下错误:
'AnonymousUser' object has no attribute 'backend'
具体步骤是:
- 我点击了连接Facebook的链接。
- 这让我跳转到了Facebook,并让我登录。我登录后,系统询问我是否允许权限,我选择了允许。
- 之后,它把我重定向回我的网站,并让我进行设置。我提供了用户名和邮箱地址。
- 当我提交后,出现了上面的错误:
错误追踪信息:
path/to_file/socialregistration/views.py in post
128. self.login(request, user)
有没有人知道,这里出了什么问题?
3 个回答
1
我刚遇到这个错误,看到这个帖子.. 我的解决办法是在注册的过程中。当用户注册时,我的接口和序列化器没有对密码进行加密.. 所以在接口视图中,我不得不手动对密码进行加密,像这样..
from django.contrib.auth.hashers import make_password
# In the register api..
@ensure_csrf_cookie
@api_view(['POST'])
def register_api(request):
# Anywhere before the serializer
request.DATA['password'] = make_password(request.DATA['password'])
# Then the serializer
serializer = RegisterSerializer(data=request.DATA)
# ... etc.. Note that if you want to login after register you will have
# to store the initial password is some buffer because.. authentication
# the none hashed version.. then
authenticate(username=request.DATA['username'], password=someBuffer)
希望这能帮助到某个人..
1
我遇到了一个新注册用户的相同错误。
def attempt_login(self, email, password):
user = authenticate(username=email, password=password)
login(self.request, user)
return user
我查看了数据库,发现用户在注册后确实创建成功,但这个错误依然存在。
我发现问题出在用户的登录名(邮箱)上,它的长度超过了30个字符,而表单字段没有进行有效性检查。这样一来,用户名在数据库中会被截断,导致系统尝试用一个不存在的登录名进行验证。
建议的邮箱字段长度是254个字符。
8
哦,伙计,我以前总是遇到这个错误。简单来说,你是在调用
self.login(request, user)
但没有先调用
authenticate(username=user, password=pwd)
这一步。
当你调用 authenticate
时,Django 会在用户身上设置一个后端属性,告诉它该用哪个后端,想了解更多细节可以看看这里
https://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth.authenticate