'AnonymousUser' object has no attribute 'backend'

前端 未结 3 1363
夕颜
夕颜 2021-01-05 17:39

Using django-socialregistration, got following error:

\'AnonymousUser\' object has no attribute \'backend\'

How,

  1. I click on f
3条回答
  •  萌比男神i
    2021-01-05 18:15

    I just got this error and found this post.. My solution was in the case was in the registration process. When the user was registering, my api and serializer wasn't hashing the password.. So in the api_view i had to manually hash the password like this..

        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)
    

    Hope that helps someone..

提交回复
热议问题