我正在尝试修改从socialaccount提供程序登录时显示用户的注册表单.
这是我的自定义注册表单代码:
from allauth.socialaccount.forms import SignupForm
from allauth.account.forms import SetPasswordField, PasswordField
class SocialPasswordedSignupForm(SignupForm):
password1 = SetPasswordField(label=_("Password"))
password2 = PasswordField(label=_("Password (again)"))
def confirm_password(self):
print('entered confirm_password')
if ("password1" in self.cleaned_data
and "password2" in self.cleaned_data):
print('password fields found')
if self.cleaned_data['password1'] != self.cleaned_data['password2']:
print('passwords not equal')
raise forms.ValidationError(_("You must type the same password"
" each time."))
print('passwords equal')
return self.cleaned_data["password1"]
else:
print('passwords not found in form')
raise forms.ValidationError(_("Password not found in form"))
def signup(self, request, user):
print('signup in SocialPasswordedSignupForm')
password = self.confirm_password()
user.set_password(password)
user.save()
settings.py:
SOCIALACCOUNT_FORMS = {
'signup': 'users.forms.SocialPasswordedSignupForm'
}
但问题是,我的注册方法永远不会被调用,因此,也没有调用confirm_password方法,并且没有对密码进行验证.也就是说,如果我输入两个不同的密码,则会保存第一个密码.
可能有什么不对?
最佳答案 您是否将此值设置为SOCIALACCOUNT_AUTO_SIGNUP = False?这是为了确保在成功验证后,用户被重定向到您的注册表单.
我来到你的链接因为我必须实现完全相同的功能.所以我就是这样做的.
forms.py
class SocialPasswordedSignupForm(SignupForm):
password1 = SetPasswordField(max_length=6,label=("Password"))
password2 = PasswordField(max_length=6, label=("Password (again)"))
#taken from https://github.com/pennersr/django-allauth/blob/master/allauth/account/forms.py
def clean_password2(self):
if ("password1" in self.cleaned_data and "password2" in self.cleaned_data):
if (self.cleaned_data["password1"] != self.cleaned_data["password2"]):
raise forms.ValidationError(("You must type the same password each time."))
return self.cleaned_data["password2"]
def signup(self, request, user):
user.set_password(self.user, self.cleaned_data["password1"])
user.save()
我有想法在https://github.com/pennersr/django-allauth/blob/master/allauth/account/forms.py中探索原始代码并发现没有像clean_password1()这样的函数,但是有clean_password2()正在完成预期的工作.所以只是按原样复制它,一切正常:)
如果它适合你,那么不要忘记接受它作为答案.