覆盖django-allauth表单字段

5
我可以帮助您翻译这段文字。该段文字涉及到IT技术,需要修改一个表单字段的属性,具体来说是登录表单:(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的表单字段? 求助!


什么是修改的意思? 您想添加新字段还是修改现有字段(例如“密码”字段)? - Dhiraj Thakur
3个回答

10

您可以使用settings.py中的ACCOUNT_FORMS来覆盖默认的登录表单,例如:

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'})

适用于登录表单和重置表单,但不适用于更改密码表单(即ACCOUNT_FORMS = {'reset_password':'XYZ'})。 - Nicholas Hamilton
你如何自定义表单中的验证规则? - Rahul Hindocha

0

我知道你可以使用ACCOUNT_SIGNUP_FORM_CLASS设置变量来覆盖注册表单类...但据我所知,没有办法更改登录表单。我在这里提出了类似的问题。


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'

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接