Skip to content

Commit

Permalink
Refs #29379 -- Moved autocomplete attribute to UsernameField.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ngnpope committed Aug 14, 2019
1 parent 0b8d911 commit 367c36b
Showing 1 changed file with 7 additions and 8 deletions.
15 changes: 7 additions & 8 deletions django/contrib/auth/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,11 @@ def to_python(self, value):
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):
Expand Down Expand Up @@ -96,10 +98,7 @@ class Meta:
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.update({'autofocus': True})

def clean_password2(self):
password1 = self.cleaned_data.get("password1")
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 367c36b

Please sign in to comment.