Ticket #10288: datetimeinput.diff

File datetimeinput.diff, 1.6 KB (added by Koen Biermans <koen.biermans@…>, 16 years ago)

fix and test for has_changed on datetimeinput widget

  • django/forms/widgets.py

     
    294294        if format:
    295295            self.format = format
    296296
    297     def render(self, name, value, attrs=None):
     297    def _format_value(self, value):
    298298        if value is None:
    299             value = ''
     299            return ''
    300300        elif hasattr(value, 'strftime'):
    301301            value = datetime_safe.new_datetime(value)
    302             value = value.strftime(self.format)
     302            return value.strftime(self.format)
     303        return value
     304
     305    def render(self, name, value, attrs=None):
     306        value = self._format_value(value)
    303307        return super(DateTimeInput, self).render(name, value, attrs)
    304308
     309    def _has_changed(self, initial, data):
     310        return super(DateTimeInput, self)._has_changed(self._format_value(initial), data)
     311
    305312class TimeInput(Input):
    306313    input_type = 'text'
    307314
  • tests/regressiontests/forms/widgets.py

     
    10931093>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51))
    10941094u'<input type="text" name="date" value="2007-09-17 12:51:00" />'
    10951095
     1096Allow custom formats:
     1097>>> w = DateTimeInput(format='%d/%m/%Y')
     1098>>> w.render('date', d)
     1099u'<input type="text" name="date" value="17/09/2007" />'
     1100>>> w._has_changed(d, '17/09/2007')
     1101False
     1102
    10961103# TimeInput ###################################################################
    10971104
    10981105>>> w = TimeInput()
Back to Top