Refs #29379 -- Moved autocomplete attribute to UsernameField.

Moving the autocomplete attribute into UsernameField allows this to work
for custom forms making use of UsernameField, removes some duplication
in the code, and keeps consistency with the autocapitalize attribute
that is already defined on UsernameField.
This commit is contained in:
Nick Pope 2019-09-02 09:50:56 +01:00 committed by Carlton Gibson
parent 47f49adc11
commit 999891bd80
1 changed files with 7 additions and 8 deletions

View File

@ -62,9 +62,11 @@ class UsernameField(forms.CharField):
return unicodedata.normalize('NFKC', super().to_python(value))
def widget_attrs(self, widget):
attrs = super().widget_attrs(widget)
attrs['autocapitalize'] = 'none'
return attrs
return {
**super().widget_attrs(widget),
'autocapitalize': 'none',
'autocomplete': 'username',
}
class UserCreationForm(forms.ModelForm):
@ -96,10 +98,7 @@ class UserCreationForm(forms.ModelForm):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self._meta.model.USERNAME_FIELD in self.fields:
self.fields[self._meta.model.USERNAME_FIELD].widget.attrs.update({
'autocomplete': 'username',
'autofocus': True,
})
self.fields[self._meta.model.USERNAME_FIELD].widget.attrs['autofocus'] = True
def clean_password2(self):
password1 = self.cleaned_data.get("password1")
@ -166,7 +165,7 @@ class AuthenticationForm(forms.Form):
Base class for authenticating users. Extend this to get a form that accepts
username/password logins.
"""
username = UsernameField(widget=forms.TextInput(attrs={'autocomplete': 'username', 'autofocus': True}))
username = UsernameField(widget=forms.TextInput(attrs={'autofocus': True}))
password = forms.CharField(
label=_("Password"),
strip=False,