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

时间秒杀一切 提交于 2019-12-02 17:51:13
Luke Sneeringer

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

jstaab

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.

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

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!