Object 'AnonymousUser' does not have attribute 'backend'

Using django-socialregistration received the following error:

'AnonymousUser' object has no attribute 'backend' 

how

  • I click on the facebook connect link.
  • It took me Facebook and asked me to log in. So I did, asked permission, I provided.
  • After that, he redirects me to my site. And ask for customization. I provide the user with an email address.
  • As soon as I submit, I get an error as above:

Trace point:

 path/to_file/socialregistration/views.py in post 128. self.login(request, user) 

Does anyone know what happened?

+6
source share
3 answers

Oh man I used to get this error all the time, basically you call

 self.login(request, user) 

without calling

authenticate(username=user, password=pwd)

first

when you call authenticate , django sets the backend attribute for the user, noting which backend to use, see here for more details https://docs.djangoproject.com/en/dev/topics/auth/#django.contrib.auth .authenticate

+8
source

I had the same error for a newly registered user.

 def attempt_login(self, email, password): user = authenticate(username=email, password=password) login(self.request, user) return user 

I registered in the database and the user was created after registration, but this error was still there.

I realized that the user login (email) was longer than 30 characters, and the form field was not checked. The username was truncated in the database, and therefore authentication was called for a nonexistent login.

254 - The character is the recommended length of the email field.

Solution: emailfield-max_length-r11092.patch

+1
source

I just got this error and found this post. My solution was what was in the registration process. When the user logged in, my api and serializer did not hash the password. So in api_view I had to manually hash a 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 someone helps ..

+1
source

Source: https://habr.com/ru/post/902724/


All Articles