我想修改表单域的属性。具体来说,登录表单:
(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表单域?帮助!
发布于 2014-09-16 15:15:11
您可以在LoginForm中使用ACCOUNT_FORMS覆盖默认settings.py,例如:
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'})发布于 2014-09-01 11:21:54
我知道您可以用ACCOUNT_SIGNUP_FORM_CLASS设置变量覆盖注册表单类...但据我所知,没有办法更改登录表单。我在这里问了我自己类似的问题。
发布于 2020-04-11 06:49:46
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'https://stackoverflow.com/questions/23580771
复制相似问题