Opened 7 years ago

Closed 7 years ago

#17170 closed Bug (duplicate)

django.forms.fields.CharField.widget_attrs wrong return

Reported by: Csaba Tóth Owned by: nobody
Component: Forms Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


The "django.forms.fields.CharField" objects method widget_attrs haven't got enough return, it should return with empty dict if the if runs to false. But i think the best if it calls the super class' method, which is yet returns an empty dict (django.forms.fields.Field)

    def widget_attrs(self, 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)}
+       else:
+           return Field.widget_attrs(self, widget)

Change History (3)

comment:1 Changed 7 years ago by Csaba Tóth

Because it returns None, this code didn't work for me:

from django import forms

class DomainRecForm(forms.Form):
    host = forms.CharField(max_length=50, label='Host Name', required=False)
    value_name = forms.CharField(max_length=500, label='Host Address (by name)', required=False, widget=forms.Textarea)

drc = DomainRecForm(data=request.POST)
h = forms.HiddenInput()
drc.fields['value_name'].widget = h

Because widget_attr(forms.HiddenInput()) returns a None object, and {}.update(None) throws an exception

comment:2 Changed 7 years ago by Julien Phalip

Triage Stage: UnreviewedAccepted

comment:3 Changed 7 years ago by Preston Timmons

Resolution: duplicate
Status: newclosed

Thanks for the report. Closing this ticket as a duplicate of #15912.

Note: See TracTickets for help on using tickets.
Back to Top