diff --git a/django/forms/fields.py b/django/forms/fields.py index 4cd2911eaad..7f8bac5052e 100644 --- a/django/forms/fields.py +++ b/django/forms/fields.py @@ -197,9 +197,11 @@ class CharField(Field): return smart_unicode(value) def widget_attrs(self, widget): + attrs = super(CharField, self).widget_attrs(widget) if self.max_length is not None and isinstance(widget, (TextInput, PasswordInput)): # The HTML attribute is maxlength, not max_length. - return {'maxlength': str(self.max_length)} + attrs.update({'maxlength': str(self.max_length)}) + return attrs class IntegerField(Field): default_error_messages = { diff --git a/tests/regressiontests/forms/tests/fields.py b/tests/regressiontests/forms/tests/fields.py index 0f9e91a55e4..7dd3a27dc1a 100644 --- a/tests/regressiontests/forms/tests/fields.py +++ b/tests/regressiontests/forms/tests/fields.py @@ -136,6 +136,23 @@ class FieldsTests(SimpleTestCase): self.assertEqual(f.max_length, None) self.assertEqual(f.min_length, 10) + def test_charfield_widget_attrs(self): + """ + Ensure that CharField.widget_attrs() always returns a dictionary. + Refs #15912 + """ + # Return an empty dictionary if max_length is None + f = CharField() + self.assertEqual(f.widget_attrs(TextInput()), {}) + + # Or if the widget is not TextInput or PasswordInput + f = CharField(max_length=10) + self.assertEqual(f.widget_attrs(HiddenInput()), {}) + + # Otherwise, return a maxlength attribute equal to max_length + self.assertEqual(f.widget_attrs(TextInput()), {'maxlength': '10'}) + self.assertEqual(f.widget_attrs(PasswordInput()), {'maxlength': '10'}) + # IntegerField ################################################################ def test_integerfield_1(self):