I want to modify attributes of a form field. Specifically, the login form:
class 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"])
How I overwrite (or override) a django-allauth form field? Help!
django-allauth is an integrated set of Django applications dealing with account authentication, registration, management, and third-party (social) account authentication. It is one of the most popular authentication modules due to its ability to handle both local and social logins.
You can overwrite the default LoginForm using ACCOUNT_FORMS
in your settings.py
, for example:
ACCOUNT_FORMS = {'login': 'yourapp.forms.YourLoginForm'}
and write a YourLoginForm
accordingly.
# 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'})
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With