我安装了 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 系统中的用户名之外。如果您想避免与用户混淆并强制用户名
字段显示电子邮件
,您还需要编辑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'
关于python - 使用 django-allauth 无法进行电子邮件身份验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49655402/