Opened 7 years ago

Closed 7 years ago

#7630 closed (fixed)

Example for custom widgets in newforms documentation is subtly wrong

Reported by: Christian Tanzer <tanzer@…> Owned by: nobody
Component: Documentation Version: master
Severity: Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation:
Needs tests: Patch needs improvement:
Easy pickings: UI/UX:


The example given for a custom widget

class CommentWidget(forms.TextInput):
    def __init__(self, *args, **kwargs):
        kwargs.setdefault('attrs',{}).update({'size': '40'})
        super(CommentWidget, self).__init__(*args, **kwargs)

will silently override further instance-specific customizations for size, e.g.,

comment = forms.CharField(
                widget=CommentWidget(attrs={'size': '80'}))

To avoid this, the example should be written like

class CommentWidget(forms.TextInput):
    def __init__(self, *args, **kwargs):
        attrs = kwargs.setdefault('attrs',{})
        if 'size' not in attrs :
            attrs['size'] = '40'
        super(CommentWidget, self).__init__(*args, **kwargs)

Change History (1)

comment:1 Changed 7 years ago by mtredinnick

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

(In [7845]) Fixed #7630 -- Slight tweak to the custom form widget exampleto avoid any
confusion. Based on a patch from Christian Tanzer.

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