diff options
| author | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2008-07-06 11:00:58 +0000 |
|---|---|---|
| committer | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2008-07-06 11:00:58 +0000 |
| commit | e2740dd400e202d9e1f60413b8abac27f7d57933 (patch) | |
| tree | b7fbe9cd1c48454ebb25dd1ca13ca23830c751a3 /docs | |
| parent | 3cfa3cbd0747c36095541177da71214289ae6aa3 (diff) | |
Fixed #7630 -- Slight tweak to the custom form widget exampleto avoid any
confusion. Based on a patch from Christian Tanzer.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@7845 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/newforms.txt | 14 |
1 files changed, 10 insertions, 4 deletions
diff --git a/docs/newforms.txt b/docs/newforms.txt index 20611249da..f0a5e1ff9f 100644 --- a/docs/newforms.txt +++ b/docs/newforms.txt @@ -1817,15 +1817,21 @@ reuse certain sets of widget attributes over and over again. Rather than repeat these attribute definitions every time you need them, Django allows you to capture those definitions as a custom widget. -For example, if you find that you are including a lot of comment fields on forms, -you could capture the idea of a ``TextInput`` with a specific ``size`` attribute -as a custom extension to the ``TextInput`` widget:: +For example, if you find that you are including a lot of comment fields on +forms, you could capture the idea of a ``TextInput`` with a specific +default ``size`` attribute as a custom extension to the ``TextInput`` widget:: class CommentWidget(forms.TextInput): def __init__(self, *args, **kwargs): - kwargs.setdefault('attrs',{}).update({'size': '40'}) + attrs = kwargs.setdefault('attrs',{}) + if 'size' not in attrs: + attrs['size'] = 40 super(CommentWidget, self).__init__(*args, **kwargs) +We allow the ``size`` attribute to be overridden by the user, but, by default, +this widget will behave as if ``attrs={'size': 40}`` was always passed into the +constructor. + Then you can use this widget in your forms:: class CommentForm(forms.Form): |
