电子邮件身份验证无法使用 django-allauth
email authentification doesn't work using django-allauth
我安装了 django-allauth 并将其设置为将其用于互联网注册和身份验证后端,以便在注册时收到电子邮件确认并使用他们的电子邮件而不是用户名注册用户。
发生的事情是注册工作完美,使用电子邮件的身份验证不起作用,而它使用用户名。我收到以下错误:
The e-mail address and/or password you specified are not correct.
这是我当前的配置:
- Django-allauth: 0.35.0
- Django: 1.11
- Python: 3.6.4
- mysql: 15.1
# settings.py
SITE_ID = 1
DJANGO_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sites',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
THIRD_PARTY_APPS = [
'allauth',
'allauth.account',
'allauth.socialaccount',
]
AUTHENTIFICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
]
LOGIN_REDIRECT_URL = 'main:home'
LOGOUT_REDIRECT_URL = 'main:home'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(APPS_DIR, 'templates'),
],
'OPTIONS': {
'debug': DEBUG,
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.media',
'django.template.context_processors.i18n',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
ACCOUNT_AUTHENTICATION_METHOD = 'username_email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_CONFIRM_EMAIL_ON_GET = True
# url.py
urlpatterns = [
# Django admin
url(settings.ADMIN_URL, admin.site.urls),
url(r'', include('main.urls')),
url(r'^accounts/', include('allauth.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
希望我提供了所有有用的信息。我想知道我是否错过了配置问题或者某处是否存在错误。如果您需要更多信息,请告诉我,我很乐意就此事提供帮助。
经过一些思考和研究,我找到了一种解决方法,即按照 the documentation 中所述编写自己的适配器。它包括重写方法 save_user()
.
为了继续,您首先需要通过编辑 settings.py
并添加以下行来告诉您的应用新适配器在哪里:
ACCOUNT_ADAPTER = 'yourproject.yourapp.file.classname'
然后创建适配器文件,例如yourproject/yourapp.file.py
,在我的例子中,我调用了file.py
adapter.py
并添加以下代码:
from allauth.account.adapter import DefaultAccountAdapter
class AccountAdapter(DefaultAccountAdapter):
def save_user(self, request, user, form, commit=True):
data = form.cleaned_data
user.email = data.get('email')
user.username = data.get('email')
if 'password1' in data:
user.set_password(data["password1"])
else:
user.set_unusable_password()
self.populate_username(request, user)
if commit:
# Ability not to commit makes it easier to derive from
# this adapter by adding
user.save()
return user
基本上,它真的很接近 actual method 除了我将电子邮件保存为 django 系统中的用户名。如果您想避免与用户混淆并强制 username
字段显示 email
.
,您还需要编辑 login.html
模板
为避免与格式错误的电子邮件地址相关的任何问题,您可以将验证器添加为 described in the documentation。
# In validators.py
from django.core.validators import EmailValidator
custom_username_validators = [ASCIIUsernameValidator()]
# In settings.py
ACCOUNT_USERNAME_VALIDATORS = 'some.module.validators.custom_username_validators'
我安装了 django-allauth 并将其设置为将其用于互联网注册和身份验证后端,以便在注册时收到电子邮件确认并使用他们的电子邮件而不是用户名注册用户。
发生的事情是注册工作完美,使用电子邮件的身份验证不起作用,而它使用用户名。我收到以下错误:
The e-mail address and/or password you specified are not correct.
这是我当前的配置:
- Django-allauth: 0.35.0
- Django: 1.11
- Python: 3.6.4
- mysql: 15.1
# settings.py
SITE_ID = 1
DJANGO_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.sites',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
THIRD_PARTY_APPS = [
'allauth',
'allauth.account',
'allauth.socialaccount',
]
AUTHENTIFICATION_BACKENDS = [
'django.contrib.auth.backends.ModelBackend',
'allauth.account.auth_backends.AuthenticationBackend',
]
LOGIN_REDIRECT_URL = 'main:home'
LOGOUT_REDIRECT_URL = 'main:home'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(APPS_DIR, 'templates'),
],
'OPTIONS': {
'debug': DEBUG,
'loaders': [
'django.template.loaders.filesystem.Loader',
'django.template.loaders.app_directories.Loader',
],
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.template.context_processors.media',
'django.template.context_processors.i18n',
'django.template.context_processors.static',
'django.template.context_processors.tz',
'django.contrib.messages.context_processors.messages',
],
},
},
]
ACCOUNT_AUTHENTICATION_METHOD = 'username_email'
ACCOUNT_EMAIL_REQUIRED = True
ACCOUNT_USERNAME_REQUIRED = False
ACCOUNT_EMAIL_VERIFICATION = 'mandatory'
ACCOUNT_CONFIRM_EMAIL_ON_GET = True
# url.py
urlpatterns = [
# Django admin
url(settings.ADMIN_URL, admin.site.urls),
url(r'', include('main.urls')),
url(r'^accounts/', include('allauth.urls')),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
希望我提供了所有有用的信息。我想知道我是否错过了配置问题或者某处是否存在错误。如果您需要更多信息,请告诉我,我很乐意就此事提供帮助。
经过一些思考和研究,我找到了一种解决方法,即按照 the documentation 中所述编写自己的适配器。它包括重写方法 save_user()
.
为了继续,您首先需要通过编辑 settings.py
并添加以下行来告诉您的应用新适配器在哪里:
ACCOUNT_ADAPTER = 'yourproject.yourapp.file.classname'
然后创建适配器文件,例如yourproject/yourapp.file.py
,在我的例子中,我调用了file.py
adapter.py
并添加以下代码:
from allauth.account.adapter import DefaultAccountAdapter
class AccountAdapter(DefaultAccountAdapter):
def save_user(self, request, user, form, commit=True):
data = form.cleaned_data
user.email = data.get('email')
user.username = data.get('email')
if 'password1' in data:
user.set_password(data["password1"])
else:
user.set_unusable_password()
self.populate_username(request, user)
if commit:
# Ability not to commit makes it easier to derive from
# this adapter by adding
user.save()
return user
基本上,它真的很接近 actual method 除了我将电子邮件保存为 django 系统中的用户名。如果您想避免与用户混淆并强制 username
字段显示 email
.
login.html
模板
为避免与格式错误的电子邮件地址相关的任何问题,您可以将验证器添加为 described in the documentation。
# In validators.py
from django.core.validators import EmailValidator
custom_username_validators = [ASCIIUsernameValidator()]
# In settings.py
ACCOUNT_USERNAME_VALIDATORS = 'some.module.validators.custom_username_validators'