Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed #1980, #2155 -- made date, time and datetime html2python method…

…s a bit

more consistent in their return values. All three now return None when the
passed in string cannot be converted to the required object (this is assumed
elsewhere).


git-svn-id: http://code.djangoproject.com/svn/django/trunk@3277 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit c2556874d4d3e2631b082f567d28cc1d1e1dfee1 1 parent 5404e6e
@malcolmt malcolmt authored
Showing with 13 additions and 10 deletions.
  1. +13 −10 django/forms/__init__.py
View
23 django/forms/__init__.py
@@ -773,15 +773,18 @@ def __init__(self, field_name, length=30, maxlength=None, is_required=False, val
def html2python(data):
"Converts the field into a datetime.datetime object"
import datetime
- date, time = data.split()
- y, m, d = date.split('-')
- timebits = time.split(':')
- h, mn = timebits[:2]
- if len(timebits) > 2:
- s = int(timebits[2])
- else:
- s = 0
- return datetime.datetime(int(y), int(m), int(d), int(h), int(mn), s)
+ try:
+ date, time = data.split()
+ y, m, d = date.split('-')
+ timebits = time.split(':')
+ h, mn = timebits[:2]
+ if len(timebits) > 2:
+ s = int(timebits[2])
+ else:
+ s = 0
+ return datetime.datetime(int(y), int(m), int(d), int(h), int(mn), s)
+ except ValueError:
+ return None
html2python = staticmethod(html2python)
class DateField(TextField):
@@ -806,7 +809,7 @@ def html2python(data):
time_tuple = time.strptime(data, '%Y-%m-%d')
return datetime.date(*time_tuple[0:3])
except (ValueError, TypeError):
- return data
+ return None
html2python = staticmethod(html2python)
class TimeField(TextField):
Please sign in to comment.
Something went wrong with that request. Please try again.