如何在自定义Django Allauth表单中设置密码字段的help_text?

时间:2019-09-19 10:48:19

标签: python django django-allauth

我想在我的自定义SignupForm(设置为help_text中的password1字段中设置ACCOUNT_SIGNUP_FORM_CLASS = 'myapp.forms.SignupForm'

否则该表单可以正常工作,但是我不能修改password1字段,因为它是Allauth从 my SignupForm继承时创建的。

我的SignupForm,在其中添加了一个复选框并调整了一些简单的操作:

class SignupForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

    class Meta:
        model = Client

        fields = [
            'phone_number',
            'pin',
            'address',
            'zipcode',
            'city',
        ]

        widgets = {
            'phone_number': phone_number_widget,
        }

    agree_to_terms = forms.BooleanField(
        label=mark_safe(
            ugettext('I agree to the <a href="/terms">terms and conditions</a>')
        )
    )

    field_order = ['name'] + Meta.fields + [
        'email', 'password1', 'password2', 'agree_to_terms'
    ]

在Allauths的account/forms.py中:

class SignupForm(BaseSignupForm):
    def __init__(self, *args, **kwargs):
        super(SignupForm, self).__init__(*args, **kwargs)
        self.fields['password1'] = PasswordField(label=_("Password"))
        if app_settings.SIGNUP_PASSWORD_ENTER_TWICE:
            self.fields['password2'] = PasswordField(
                label=_("Password (again)"))

    ...
    ...

有没有办法解决这个问题?

1 个答案:

答案 0 :(得分:0)

您的SignupForm应该是allauth SignupForm的子类,这样它将从BaseSignupForm继承,而不是BaseSignupForm从您的表单继承:

from allauth.account.forms import SignupForm as AllauthSignupForm
class SignupForm(ModelForm, AllauthSignupForm):
    ...

然后在设置中设置ACCOUNT_FORMS = {'signup': 'my_app.forms.SignupForm'}