From 999891bd80b3d02dd916731a7a239e1036174885 Mon Sep 17 00:00:00 2001 From: Nick Pope Date: Mon, 2 Sep 2019 09:50:56 +0100 Subject: [PATCH] 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. --- django/contrib/auth/forms.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/django/contrib/auth/forms.py b/django/contrib/auth/forms.py index 7fbe674948..a0cfed0995 100644 --- a/django/contrib/auth/forms.py +++ b/django/contrib/auth/forms.py @@ -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,