Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[1.0.X] Fixed #10288 -- Corrected _has_changed handling of DateTimeIn…

…put when a custom date/time format is in use. Thanks to Koen Biermans for the report and patch.

Merge of r10641 from trunk.

git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@10757 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 17d214a9821542696b4b632b1fc70086ed2f7ecc 1 parent cc96ed9
Russell Keith-Magee freakboy3742 authored
Showing with 24 additions and 10 deletions.
  1. +15 −8 django/forms/widgets.py
  2. +9 −2 tests/regressiontests/forms/widgets.py
23 django/forms/widgets.py
View
@@ -295,14 +295,21 @@ def __init__(self, attrs=None, format=None):
if format:
self.format = format
- def render(self, name, value, attrs=None):
+ def _format_value(self, value):
if value is None:
- value = ''
+ return ''
elif hasattr(value, 'strftime'):
value = datetime_safe.new_datetime(value)
- value = value.strftime(self.format)
+ return value.strftime(self.format)
+ return value
+
+ def render(self, name, value, attrs=None):
+ value = self._format_value(value)
return super(DateTimeInput, self).render(name, value, attrs)
+ def _has_changed(self, initial, data):
+ return super(DateTimeInput, self)._has_changed(self._format_value(initial), data)
+
class TimeInput(Input):
input_type = 'text'
@@ -400,11 +407,11 @@ def render(self, name, value, attrs=None, choices=()):
def value_from_datadict(self, data, files, name):
value = data.get(name, None)
- return {u'2': True,
- True: True,
- 'True': True,
- u'3': False,
- 'False': False,
+ return {u'2': True,
+ True: True,
+ 'True': True,
+ u'3': False,
+ 'False': False,
False: False}.get(value, None)
def _has_changed(self, initial, data):
11 tests/regressiontests/forms/widgets.py
View
@@ -1071,9 +1071,9 @@
>>> w.render('date', datetime.datetime(2006, 1, 10, 7, 30))
u'<input type="text" class="pretty" value="2006-01-10" name="date_0" /><input type="text" class="pretty" value="07:30:00" name="date_1" />'
->>> w._has_changed(datetime.datetime(2008, 5, 5, 12, 40, 00), [u'2008-05-05', u'12:40:00'])
+>>> w._has_changed(datetime.datetime(2008, 5, 6, 12, 40, 00), [u'2008-05-06', u'12:40:00'])
False
->>> w._has_changed(datetime.datetime(2008, 5, 5, 12, 40, 00), [u'2008-05-05', u'12:41:00'])
+>>> w._has_changed(datetime.datetime(2008, 5, 6, 12, 40, 00), [u'2008-05-06', u'12:41:00'])
True
# DateTimeInput ###############################################################
@@ -1093,6 +1093,13 @@
>>> w.render('date', datetime.datetime(2007, 9, 17, 12, 51))
u'<input type="text" name="date" value="2007-09-17 12:51:00" />'
+Use 'format' to change the way a value is displayed.
+>>> w = DateTimeInput(format='%d/%m/%Y %H:%M')
+>>> w.render('date', d)
+u'<input type="text" name="date" value="17/09/2007 12:51" />'
+>>> w._has_changed(d, '17/09/2007 12:51')
+False
+
# TimeInput ###################################################################
>>> w = TimeInput()
Please sign in to comment.
Something went wrong with that request. Please try again.