Django

Code

Changeset 7845

Show
Ignore:
Timestamp:
07/06/08 06:00:58 (5 months ago)
Author:
mtredinnick
Message:

Fixed #7630 -- Slight tweak to the custom form widget exampleto avoid any
confusion. Based on a patch from Christian Tanzer.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • django/trunk/docs/newforms.txt

    r7838 r7845  
    18181818you to capture those definitions as a custom widget. 
    18191819 
    1820 For example, if you find that you are including a lot of comment fields on forms, 
    1821 you could capture the idea of a ``TextInput`` with a specific ``size`` attribute 
    1822 as a custom extension to the ``TextInput`` widget:: 
     1820For example, if you find that you are including a lot of comment fields on 
     1821forms, you could capture the idea of a ``TextInput`` with a specific 
     1822default ``size`` attribute as a custom extension to the ``TextInput`` widget:: 
    18231823 
    18241824    class CommentWidget(forms.TextInput): 
    18251825        def __init__(self, *args, **kwargs): 
    1826             kwargs.setdefault('attrs',{}).update({'size': '40'}) 
     1826            attrs = kwargs.setdefault('attrs',{}) 
     1827            if 'size' not in attrs: 
     1828                attrs['size'] = 40 
    18271829            super(CommentWidget, self).__init__(*args, **kwargs) 
     1830 
     1831We allow the ``size`` attribute to be overridden by the user, but, by default, 
     1832this widget will behave as if ``attrs={'size': 40}`` was always passed into the 
     1833constructor. 
    18281834 
    18291835Then you can use this widget in your forms::