【问题标题】:Overwrite django-allauth form field覆盖 django-allauth 表单域
【发布时间】:2014-06-28 03:32:20
【问题描述】:

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

(django-allauth 登录表单)

类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 表单域?救命!

【问题讨论】:

  • 修改是什么意思?您要添加新字段还是修改现有字段,例如password 字段?

标签: django forms field overriding django-allauth


【解决方案1】:

您可以在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'}
  • 如何自定义表单中的验证规则?
【解决方案2】:

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

【讨论】:

    【解决方案3】:
    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'
    

    【讨论】:

      猜你喜欢
      • 2014-01-05
      • 2016-09-16
      • 2016-07-29
      • 2016-10-17
      • 2014-10-25
      • 2014-06-08
      • 2019-07-02
      • 2014-08-30
      • 2023-03-06
      相关资源
      最近更新 更多