Opened 16 years ago
Closed 16 years ago
#7630 closed (fixed)
Example for custom widgets in newforms documentation is subtly wrong
Reported by: | Owned by: | nobody | |
---|---|---|---|
Component: | Documentation | Version: | dev |
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 |
Description
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)
Note:
See TracTickets
for help on using tickets.
(In [7845]) Fixed #7630 -- Slight tweak to the custom form widget exampleto avoid any
confusion. Based on a patch from Christian Tanzer.