首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >覆盖django-allauth表单域

覆盖django-allauth表单域
EN

Stack Overflow用户
提问于 2014-05-10 19:57:53
回答 3查看 2.7K关注 0票数 5

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

(django-allauth LoginForm)

LoginForm类(forms.Form):

代码语言:javascript
复制
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表单域?帮助!

EN

回答 3

Stack Overflow用户

发布于 2014-09-16 15:15:11

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

代码语言:javascript
复制
ACCOUNT_FORMS = {'login': 'yourapp.forms.YourLoginForm'}

并相应地编写YourLoginForm

代码语言:javascript
复制
# 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'})
票数 8
EN

Stack Overflow用户

发布于 2014-09-01 11:21:54

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

票数 0
EN

Stack Overflow用户

发布于 2020-04-11 06:49:46

代码语言:javascript
复制
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'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23580771

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档