Django-AttributeError 'User' object has no attribute 'backend' (But…it does?)

前端 未结 3 2027
逝去的感伤
逝去的感伤 2021-01-31 07:28

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 v

相关标签:
3条回答
  • 2021-01-31 07:50

    I'll post this as an answer, But I owe it to https://stackoverflow.com/users/558699/ben in the comments above, and https://stackoverflow.com/a/5837046/1467342. I was scanning this question and missed that what I was looking for was in the comments. Adding a backend manually has been a (hacky) fix for me twice so far:

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

    In both cases, I'm relying on other authentication methods (email confirmation and admin authenticated session) for verifying permission to log in as this user.

    0 讨论(0)
  • 2021-01-31 07:59

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

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

    0 讨论(0)
  • 2021-01-31 08:09

    in Django 1.10, django.contrib.auth.login now takes a backend= keyword argument!

    https://code.djangoproject.com/ticket/24855

    0 讨论(0)
提交回复
热议问题