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