自定义Django allauth的社交帐户注册表单:添加密码字段 [英] Customizing Django allauth's socialaccount signup form: adding password fields
问题描述
我正在尝试修改从社交帐户提供商登录时向用户显示的注册表单。
I'm trying to modify the signup form which the user is shown when logging in from a socialaccount provider.
这是我自定义的注册表单代码:
Here's me custom signup form code:
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:
settings.py:
SOCIALACCOUNT_FORMS = {
'signup': 'users.forms.SocialPasswordedSignupForm'
}
但是问题是,我的注册方法从没有被调用,因此 confirm_password
方法也不会被调用,并且也不会验证密码。也就是说,如果我输入两个不同的密码,则会保存第一个密码。
But the problem is, that my signup method never gets called, and so, the confirm_password
method isn't called either, and there's no validation on password being done. That is, if I enter two different passwords, the first password is saved.
有什么问题吗?
推荐答案
您是否设置了此值SOCIALACCOUNT_AUTO_SIGNUP = False?这是为了确保在成功通过身份验证后将用户重定向到您的注册表单。
Are you setting this value SOCIALACCOUNT_AUTO_SIGNUP = False ? This is to make sure that upon successful authentication the user is redirected to your signup form.
我进入您的链接是因为我必须实现完全相同的功能。因此,这就是我这样做的方式。
I came to your link because I had to implement exact same feature. So this is how I have done it on my end.
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()正在完成预期的工作。因此,只要照原样复制它,一切都可以:)
I got the idea to explore the original code in https://github.com/pennersr/django-allauth/blob/master/allauth/account/forms.py and found out that there is no such function like clean_password1() but there is clean_password2() which is doing the intended work. So just copied it as it is and everything worked :)
如果它对您有用,那么别忘了接受它作为答案。
If it works for you then don't forget to accept it as answer.
这篇关于自定义Django allauth的社交帐户注册表单:添加密码字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!