On Mar 18, 9:55 am, "johnny" <[EMAIL PROTECTED]> wrote:
> At least, can someone tell me, what date time format, newforms
> DateTimeField takes? Right now it's not taking 03/17/2007 05:11 PM in
> the input field, validation is failing.
The problem is you are specifying 'PM'. None of the default filters
deal with am/pm.
The parsing of the datetime newform field is configurable if you do
not like the ones provided by default.
I would recommend creating a new list based on the default one in
django.newforms.fields.DEFAULT_DATETTIME_INPUT_FORMATS
The strings in that tuple are used to convert the form input using
time.strptime().
Below is 100% of the code used for DateTimeField. It should be pretty
easy to understand what is going on.
If you need immediate answers to your questions (i.e. sooner than a
day or two) I would recommend going on irc to #django
(irc.freenode.net).
>From django/newforms/fields.py:
DEFAULT_DATETIME_INPUT_FORMATS = (
'%Y-%m-%d %H:%M:%S', # '2006-10-25 14:30:59'
'%Y-%m-%d %H:%M', # '2006-10-25 14:30'
'%Y-%m-%d', # '2006-10-25'
'%m/%d/%Y %H:%M:%S', # '10/25/2006 14:30:59'
'%m/%d/%Y %H:%M', # '10/25/2006 14:30'
'%m/%d/%Y', # '10/25/2006'
'%m/%d/%y %H:%M:%S', # '10/25/06 14:30:59'
'%m/%d/%y %H:%M', # '10/25/06 14:30'
'%m/%d/%y', # '10/25/06'
)
class DateTimeField(Field):
def __init__(self, input_formats=None, *args, **kwargs):
super(DateTimeField, self).__init__(*args, **kwargs)
self.input_formats = input_formats or
DEFAULT_DATETIME_INPUT_FORMATS
def clean(self, value):
"""
Validates that the input can be converted to a datetime.
Returns a
Python datetime.datetime object.
"""
super(DateTimeField, self).clean(value)
if value in EMPTY_VALUES:
return None
if isinstance(value, datetime.datetime):
return value
if isinstance(value, datetime.date):
return datetime.datetime(value.year, value.month,
value.day)
for format in self.input_formats:
try:
return datetime.datetime(*time.strptime(value, format)
[:6])
except ValueError:
continue
raise ValidationError(gettext(u'Enter a valid date/time.'))
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---