Ticket #16630: 16630-input_type.diff

File 16630-input_type.diff, 6.7 KB (added by Claude Paroz, 12 years ago)

Made input_type customizable

  • django/forms/fields.py

    diff --git a/django/forms/fields.py b/django/forms/fields.py
    index 7f0d26d..124e4f6 100644
    a b class CharField(Field):  
    199199
    200200    def widget_attrs(self, widget):
    201201        attrs = super(CharField, self).widget_attrs(widget)
    202         if self.max_length is not None and isinstance(widget, (TextInput, PasswordInput)):
     202        if self.max_length is not None and isinstance(widget, TextInput):
    203203            # The HTML attribute is maxlength, not max_length.
    204204            attrs.update({'maxlength': str(self.max_length)})
    205205        return attrs
  • django/forms/widgets.py

    diff --git a/django/forms/widgets.py b/django/forms/widgets.py
    index 7651efc..f8af9e0 100644
    a b class Input(Widget):  
    260260            final_attrs['value'] = force_text(self._format_value(value))
    261261        return format_html('<input{0} />', flatatt(final_attrs))
    262262
     263
    263264class TextInput(Input):
    264265    input_type = 'text'
    265266
    266 class PasswordInput(Input):
     267    def __init__(self, attrs=None, input_type=None):
     268        if input_type is not None:
     269            self.input_type = input_type
     270        super(TextInput, self).__init__(attrs)
     271
     272
     273class PasswordInput(TextInput):
    267274    input_type = 'password'
    268275
    269276    def __init__(self, attrs=None, render_value=False):
    class Textarea(Widget):  
    400407                           flatatt(final_attrs),
    401408                           force_text(value))
    402409
    403 class DateInput(Input):
    404     input_type = 'text'
    405410
    406     def __init__(self, attrs=None, format=None):
    407         super(DateInput, self).__init__(attrs)
     411class DateInput(TextInput):
     412    def __init__(self, attrs=None, format=None, **kwargs):
     413        super(DateInput, self).__init__(attrs, **kwargs)
    408414        if format:
    409415            self.format = format
    410416            self.manual_format = True
    class DateInput(Input):  
    431437            pass
    432438        return super(DateInput, self)._has_changed(self._format_value(initial), data)
    433439
    434 class DateTimeInput(Input):
    435     input_type = 'text'
    436440
    437     def __init__(self, attrs=None, format=None):
    438         super(DateTimeInput, self).__init__(attrs)
     441class DateTimeInput(TextInput):
     442    def __init__(self, attrs=None, format=None, **kwargs):
     443        super(DateTimeInput, self).__init__(attrs, **kwargs)
    439444        if format:
    440445            self.format = format
    441446            self.manual_format = True
    class DateTimeInput(Input):  
    462467            pass
    463468        return super(DateTimeInput, self)._has_changed(self._format_value(initial), data)
    464469
    465 class TimeInput(Input):
    466     input_type = 'text'
    467470
    468     def __init__(self, attrs=None, format=None):
    469         super(TimeInput, self).__init__(attrs)
     471class TimeInput(TextInput):
     472    def __init__(self, attrs=None, format=None, **kwargs):
     473        super(TimeInput, self).__init__(attrs, **kwargs)
    470474        if format:
    471475            self.format = format
    472476            self.manual_format = True
  • docs/ref/forms/widgets.txt

    diff --git a/docs/ref/forms/widgets.txt b/docs/ref/forms/widgets.txt
    index eab314a..96187e2 100644
    a b commonly used groups of widgets:  
    178178
    179179    Text input: ``<input type='text' ...>``
    180180
     181    Takes one optional argument:
     182
     183    .. attribute:: TextInput.input_type
     184
     185        The content of the type attribute of the input element (defaults to
     186        'text').
     187
     188    .. versionchanged:: 1.5
     189        The `input_type` argument has been added
     190
    181191``PasswordInput``
    182192~~~~~~~~~~~~~~~~~
    183193
    commonly used groups of widgets:  
    245255
    246256    Date input as a simple text box: ``<input type='text' ...>``
    247257
    248     Takes one optional argument:
     258    Takes same arguments as :class:`TextInput`, with one more optional argument:
    249259
    250260    .. attribute:: DateInput.format
    251261
    commonly used groups of widgets:  
    262272
    263273    Date/time input as a simple text box: ``<input type='text' ...>``
    264274
    265     Takes one optional argument:
     275    Takes same arguments as :class:`TextInput`, with one more optional argument:
    266276
    267277    .. attribute:: DateTimeInput.format
    268278
    commonly used groups of widgets:  
    279289
    280290    Time input as a simple text box: ``<input type='text' ...>``
    281291
    282     Takes one optional argument:
     292    Takes same arguments as :class:`TextInput`, with one more optional argument:
    283293
    284294    .. attribute:: TimeInput.format
    285295
  • tests/regressiontests/forms/tests/widgets.py

    diff --git a/tests/regressiontests/forms/tests/widgets.py b/tests/regressiontests/forms/tests/widgets.py
    index 544ca41..e062b9f 100644
    a b class FormsWidgetTestCase(TestCase):  
    3434        w = TextInput(attrs={'class': 'fun'})
    3535        self.assertHTMLEqual(w.render('email', ''), '<input type="text" class="fun" name="email" />')
    3636        self.assertHTMLEqual(w.render('email', 'foo@example.com'), '<input type="text" class="fun" value="foo@example.com" name="email" />')
     37        # ... or input_type
     38        w = TextInput(input_type='hello')
     39        self.assertHTMLEqual(w.render('generic', ''), u'<input type="hello" name="generic" />')
    3740
    3841        # 'attrs' passed to render() get precedence over those passed to the constructor:
    3942        w = TextInput(attrs={'class': 'pretty'})
    beatle J R Ringo False""")  
    919922        self.assertHTMLEqual(w.render('date', d), '<input type="text" name="date" value="17/09/2007 12:51" />')
    920923        self.assertFalse(w._has_changed(d, '17/09/2007 12:51'))
    921924
     925        w = DateTimeInput(input_type='datetime')
     926        self.assertHTMLEqual(w.render('date', None), '<input type="datetime" name="date" />')
     927
    922928        # Make sure a custom format works with _has_changed. The hidden input will use
    923929        data = datetime.datetime(2010, 3, 6, 12, 0, 0)
    924930        custom_format = '%d.%m.%Y %H:%M'
    beatle J R Ringo False""")  
    942948        self.assertHTMLEqual(w.render('date', d), '<input type="text" name="date" value="17/09/2007" />')
    943949        self.assertFalse(w._has_changed(d, '17/09/2007'))
    944950
     951        w = DateInput(input_type='date')
     952        self.assertHTMLEqual(w.render('date', None), '<input type="date" name="date" />')
     953
    945954        # Make sure a custom format works with _has_changed. The hidden input will use
    946955        data = datetime.date(2010, 3, 6)
    947956        custom_format = '%d.%m.%Y'
    beatle J R Ringo False""")  
    967976        self.assertHTMLEqual(w.render('time', t), '<input type="text" name="time" value="12:51" />')
    968977        self.assertFalse(w._has_changed(t, '12:51'))
    969978
     979        w = DateInput(input_type='time')
     980        self.assertHTMLEqual(w.render('time', None), '<input type="time" name="time" />')
     981
    970982        # Make sure a custom format works with _has_changed. The hidden input will use
    971983        data = datetime.time(13, 0)
    972984        custom_format = '%I:%M %p'
Back to Top