定制Django allauth';s socialaccount注册表单:添加密码字段

定制Django allauth';s socialaccount注册表单:添加密码字段,django,django-allauth,Django,Django Allauth,我试图修改用户从socialaccount提供商登录时显示的注册表单 这是我的自定义注册表单代码: from allauth.socialaccount.forms import SignupForm from allauth.account.forms import SetPasswordField, PasswordField class SocialPasswordedSignupForm(SignupForm): password1 = SetPasswordField(l

我试图修改用户从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\u password
方法也没有被调用,并且没有对密码进行验证。也就是说,如果我输入两个不同的密码,则会保存第一个密码


可能有什么问题?

您是否将此值设置为SOCIALACCOUNT\u AUTO\u 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()
我开始研究中的原始代码,发现没有像clean_password1()这样的函数,但是有clean_password2()可以完成预期的工作。所以只要照原样复制,一切都正常:)


如果它对您有效,请不要忘记接受它作为答案。

我基本上创建了我的SignupForm类版本:

from allauth.account.forms import SetPasswordField, PasswordField
from allauth.account import app_settings
from allauth.account.utils import user_field, user_email, user_username
from django.utils.translation import ugettext_lazy as _


class SocialPasswordedSignupForm(BaseSignupForm):

    password1 = SetPasswordField(label=_("Password"))
    password2 = SetPasswordField(label=_("Confirm Password"))

    def __init__(self, *args, **kwargs):
        self.sociallogin = kwargs.pop('sociallogin')
        user = self.sociallogin.user
        # TODO: Should become more generic, not listing
        # a few fixed properties.
        initial = {'email': user_email(user) or '',
                   'username': user_username(user) or '',
                   'first_name': user_field(user, 'first_name') or '',
                   'last_name': user_field(user, 'last_name') or ''}
        kwargs.update({
            'initial': initial,
            'email_required': kwargs.get('email_required',
                                         app_settings.EMAIL_REQUIRED)})
        super(SocialPasswordedSignupForm, self).__init__(*args, **kwargs)

    def save(self, request):
        adapter = get_adapter()
        user = adapter.save_user(request, self.sociallogin, form=self)
        self.custom_signup(request, user)
        return user

    def clean(self):
        super(SocialPasswordedSignupForm, self).clean()
        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."))

    def raise_duplicate_email_error(self):
        raise forms.ValidationError(
            _("An account already exists with this e-mail address."
              " Please sign in to that account first, then connect"
              " your %s account.")
            % self.sociallogin.account.get_provider().name)

    def custom_signup(self, request, user):
        password = self.cleaned_data['password1']
        user.set_password(password)
        user.save()
为我工作,完美无瑕。你们可以比较socialaccount.forms中的默认注册表单和我的实现中的差异