Opened 11 years ago

Closed 11 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: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


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 11 years ago by Malcolm Tredinnick

Resolution: fixed
Status: newclosed

(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