Django使用多个backends的小问题

用户面临登录的时候,可以有不同的登入方式,可以手机号,邮箱,用户名,第三方登录等。那么我们就需要重写后端认证类。但是有些情况下会出现多个backend认证。使用login()方法的时候,会报错。

具体错误:

You have multiple authentication backends configured and therefore must provide the backendargument or set thebackend attribute on the user.

该错误指明了我在配置文件中配置了多个backend,那么我在使用login()的时候需要指定使用哪一个backend。

AUTHENTICATION_BACKENDS = ['e_mall.authentication_consumer.Email_or_Username', 'e_mall.authentication_consumer.Phone']

解决方案:

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

指定backend就好了。