如何覆盖allauth的SignupForm类,以便我可以删除emailfield的标签?

时间:2016-08-05 06:09:18

标签: django django-forms django-allauth

Screenshot of the rendered form

我已完成所有必要的导入,但代码很好,但即使将其设置为false,emailfield的标签仍会显示。

class SignupForm(forms.Form):
    def __init__(self, *args, **kwargs):
        super(SignupForm, self).__init__(*args, **kwargs)
        self.fields['username'].label = False
        self.fields['password1'].label = False
        self.fields['password2'].label = False
        self.fields['email'].label = False

    def signup(self, request, user):
        user.username = self.cleaned_data['username']
        user.email = self.cleaned_data['email']
        user.password = self.cleaned_data['password']
        user.save(commit=False)

这是allauth / accounts / forms.py

class BaseSignupForm(_base_signup_form_class()):
    username = forms.CharField(label=_("Username"),
                           min_length=app_settings.USERNAME_MIN_LENGTH,
                           widget=forms.TextInput(
                               attrs={'placeholder':
                                      _('Username'),
                                      'autofocus': 'autofocus'}))
    email = forms.EmailField(widget=forms.TextInput(
        attrs={'type': 'email',
           'placeholder': _('E-mail address')}))

    def __init__(self, *args, **kwargs):
        email_required = kwargs.pop('email_required',
                                app_settings.EMAIL_REQUIRED)
        self.username_required = kwargs.pop('username_required',
                                        app_settings.USERNAME_REQUIRED)
        super(BaseSignupForm, self).__init__(*args, **kwargs)
        username_field = self.fields['username']
        username_field.max_length = get_username_max_length()
        username_field.validators.append(
            validators.MaxLengthValidator(username_field.max_length))

        # field order may contain additional fields from our base class,
        # so take proper care when reordering...
        field_order = ['email', 'username']
        merged_field_order = list(self.fields.keys())
        if email_required:
            self.fields["email"].label = ugettext("E-mail")
            self.fields["email"].required = True
        else:
            self.fields["email"].label = ugettext("E-mail (optional)")
            self.fields["email"].required = False
            self.fields["email"].widget.is_required = False
            if self.username_required:
                field_order = ['username', 'email']...

我相信“if email_required”是个问题。任何帮助显示我做错了将不胜感激。感谢

1 个答案:

答案 0 :(得分:0)

我终于按照我的意愿进行了渲染。我用过:

{{1}}

和{%crispy form%}标记。