From 003b3c1a17e2ae54fe24b7f534469f8b661e2727 Mon Sep 17 00:00:00 2001 From: Malcolm Tredinnick Date: Mon, 22 Oct 2007 13:13:12 +0000 Subject: [PATCH] Fixed #5794 -- Be more robust when rendering a DateTimeInput widget. Thanks, MikeH. git-svn-id: http://code.djangoproject.com/svn/django/trunk@6594 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/newforms/widgets.py | 7 +++++-- tests/regressiontests/forms/widgets.py | 2 ++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/django/newforms/widgets.py b/django/newforms/widgets.py index c3e4b09d93..076845427d 100644 --- a/django/newforms/widgets.py +++ b/django/newforms/widgets.py @@ -161,8 +161,11 @@ class DateTimeInput(Input): self.format = format def render(self, name, value, attrs=None): - return super(DateTimeInput, self).render(name, - value.strftime(self.format), attrs) + if value is None: + value = '' + elif hasattr(value, 'strftime'): + value = value.strftime(self.format) + return super(DateTimeInput, self).render(name, value, attrs) class CheckboxInput(Widget): def __init__(self, attrs=None, check_test=bool): diff --git a/tests/regressiontests/forms/widgets.py b/tests/regressiontests/forms/widgets.py index 032f3cbb91..cb1d084631 100644 --- a/tests/regressiontests/forms/widgets.py +++ b/tests/regressiontests/forms/widgets.py @@ -855,6 +855,8 @@ u'>> w = DateTimeInput() +>>> w.render('date', None) +u'' >>> d = datetime.datetime(2007, 9, 17, 12, 51, 34, 482548) >>> print d 2007-09-17 12:51:34.482548