Django休息auth电子邮件而不是用户名

时间:2016-04-18 15:34:56

标签: python django django-rest-framework django-allauth django-rest-auth

我有一个django项目,我在其中使用Django-rest-auth进行身份验证。我想使用 电子邮件密码 来验证用户身份,而不是用户名+密码。

我在settings.py中有以下设置,但它没有为我做任何事情:

REST_SESSION_LOGIN = True
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_AUTHENTICATION_METHOD = 'EMAIL'
ACCOUNT_EMAIL_VERIFICATION = 'optional'

我怎样才能实现它?

2 个答案:

答案 0 :(得分:14)

以下设置工作:

#This is required otherwise it asks for email server
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'
# ACCOUNT_EMAIL_REQUIRED = True
# AUTHENTICATION_METHOD = 'EMAIL'
# ACCOUNT_EMAIL_VERIFICATION = 'optional'

ACCOUNT_AUTHENTICATION_METHOD = 'email'
ACCOUNT_EMAIL_REQUIRED = True   
ACCOUNT_USERNAME_REQUIRED = False

#Following is added to enable registration with email instead of username
AUTHENTICATION_BACKENDS = (
 # Needed to login by username in Django admin, regardless of `allauth`
 "django.contrib.auth.backends.ModelBackend",

 # `allauth` specific authentication methods, such as login by e-mail
 "allauth.account.auth_backends.AuthenticationBackend",
)

答案 1 :(得分:0)

我也在使用这个软件包,通过调用这个配置它对我有用:

ACCOUNT_AUTHENTICATION_METHOD = 'email'

请注意此配置,此配置属于django-allauth,请参阅:

class AuthenticationMethod:
    USERNAME = 'username'
    EMAIL = 'email'
    USERNAME_EMAIL = 'username_email'

上面的类是allauth中的设置,所以你用小写字母写'EMAIL'。

我希望它有所帮助