Code

Opened 3 years ago

Closed 3 years ago

#17170 closed Bug (duplicate)

django.forms.fields.CharField.widget_attrs wrong return

Reported by: tsabi 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

Description

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)

Attachments (0)

Change History (3)

comment:1 Changed 3 years ago by tsabi

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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()
h.attrs.update(drc.fields['value_ip'].widget_attrs(h))
drc.fields['value_name'].widget = h

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

comment:2 Changed 3 years ago by julien

  • Triage Stage changed from Unreviewed to Accepted

comment:3 Changed 3 years ago by prestontimmons

  • Resolution set to duplicate
  • Status changed from new to closed

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

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.