覆盖django-allauth表单字段

时间:2014-05-10 11:57:53

标签: django forms field override django-allauth

我想修改表单字段的属性。具体来说,登录表单:

(django-allauth LoginForm)

类LoginForm(forms.Form):

password = PasswordField(label=_("Password"))
remember = forms.BooleanField(label=_("Remember Me"),
                              required=False)

user = None

def __init__(self, *args, **kwargs):
    super(LoginForm, self).__init__(*args, **kwargs)
    if app_settings.AUTHENTICATION_METHOD == AuthenticationMethod.EMAIL:
        login_widget = forms.TextInput(attrs={'type': 'email',
                                              'placeholder':
                                              _('E-mail address'),
                                              'autofocus': 'autofocus'})
        login_field = forms.EmailField(label=_("E-mail"),
                                       widget=login_widget)
    elif app_settings.AUTHENTICATION_METHOD \
            == AuthenticationMethod.USERNAME:
        login_widget = forms.TextInput(attrs={'placeholder':
                                              _('Username'),
                                              'autofocus': 'autofocus'})
        login_field = forms.CharField(label=_("Username"),
                                      widget=login_widget,
                                      max_length=30)
    else:
        assert app_settings.AUTHENTICATION_METHOD \
            == AuthenticationMethod.USERNAME_EMAIL
        login_widget = forms.TextInput(attrs={'placeholder':
                                              _('Username or e-mail'),
                                              'autofocus': 'autofocus'})
        login_field = forms.CharField(label=pgettext("field label",
                                                     "Login"),
                                      widget=login_widget)
    self.fields["login"] = login_field
    set_form_field_order(self,  ["login", "password", "remember"])

我如何覆盖(或覆盖)django-allauth表单字段?救命啊!

3 个答案:

答案 0 :(得分:8)

您可以使用ACCOUNT_FORMS中的settings.py覆盖默认的LoginForm,例如:

ACCOUNT_FORMS = {'login': 'yourapp.forms.YourLoginForm'}

并相应地写一个YourLoginForm

# yourapp/forms.py

from allauth.account.forms import LoginForm

class YourLoginForm(LoginForm):
    def __init__(self, *args, **kwargs):
        super(YourLoginForm, self).__init__(*args, **kwargs)
        self.fields['login'].widget = forms.TextInput(attrs={'type': 'email', 'class': 'yourclass'})
        self.fields['password'].widget = forms.PasswordInput(attrs={'class': 'yourclass'})

答案 1 :(得分:0)

我知道您可以使用ACCOUNT_SIGNUP_FORM_CLASS设置变量覆盖注册表单类...但据我所知,无法更改登录表单。我在这里问了自己类似的问题。

答案 2 :(得分:0)

class SignupForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
    super(SignupForm, self).__init__(*args, **kwargs)
    self.fields['first_name'].widget = forms.TextInput(attrs={'placeholder': 'Enter first name'})
    self.fields['last_name'].widget = forms.TextInput(attrs={'placeholder': 'Enter last name'})

#settings.py or base.py    
ACCOUNT_SIGNUP_FORM_CLASS = 'NameApp.forms.SignupForm'