Opened 13 years ago

Closed 12 years ago

#18519 closed Bug (invalid)

DateField ignores DATE_INPUT_FORMATS (because get_format ignores it)

Reported by: marcin.tustin@… Owned by: nobody
Component: Forms Version: 1.4
Severity: Normal Keywords: DateField, django.utils.formats
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description

DateFields in forms do not have their behaviour altered by the setting DATE_INPUT_FORMATS.

This happens because django.utils.get_format ignores it.

Here is a transcript of a debug session demonstrating that:

In [14]: df.to_python('13-03-1978')
---------------------------------------------------------------------------
ValidationError                           Traceback (most recent call last)
/home/marcintustin/webapps/django/oneclickcosvirt/lib/python2.7/site-packages/django/core/management/commands/shell.pyc in <module>()
----> 1 df.to_python('13-03-1978')

/home/marcintustin/webapps/django/oneclickcosvirt/lib/python2.7/site-packages/django/forms/fields.pyc in to_python(self, value)
    377         if isinstance(value, datetime.date):
    378             return value
--> 379         return super(DateField, self).to_python(value)
    380
    381     def strptime(self, value, format):

/home/marcintustin/webapps/django/oneclickcosvirt/lib/python2.7/site-packages/django/forms/fields.pyc in to_python(self, value)
    354                         except ValueError:
    355                             continue
--> 356         raise ValidationError(self.error_messages['invalid'])
    357
    358     def strptime(self, value, format):

ValidationError: [u'Enter a valid date.']

In [15]: import pdb; pdb.pm()
> /home/marcintustin/webapps/django/oneclickcosvirt/lib/python2.7/site-packages/django/forms/fields.py(356)to_python()
-> raise ValidationError(self.error_messages['invalid'])
(Pdb) l
351                                 usecs = int(usecs_str[:6].ljust(6, '0'))
352                                 dt = datetime.datetime.strptime(datetime_str, format[:-3])
353                                 return dt.replace(microsecond=usecs)
354                             except ValueError:
355                                 continue
356  ->         raise ValidationError(self.error_messages['invalid'])
357
358         def strptime(self, value, format):
359             raise NotImplementedError('Subclasses must define this method.')
360
361     class DateField(BaseTemporalField):
(Pdb) format
'%m/%d/%y'
(Pdb) self.input_formats
<django.utils.functional.__proxy__ object at 0x20da390>
(Pdb) list(self.input_formats)
*** Error in argument: '(self.input_formats)'
(Pdb) self.input_formats
<django.utils.functional.__proxy__ object at 0x20da390>
(Pdb) tuple(self.input_formats)
('%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y')
(Pdb) self
<django.forms.fields.DateField object at 0x347fdd0>
(Pdb) formats
<module 'django.utils.formats' from '/home/marcintustin/webapps/django/oneclickcosvirt/lib/python2.7/site-packages/django/utils/formats.pyc'>
(Pdb) formats.get_format_lazy('DATE_INPUT_FORMATS')
<django.utils.functional.__proxy__ object at 0x3537650>
(Pdb) tuple(formats.get_format_lazy('DATE_INPUT_FORMATS'))
('%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y')
(Pdb) from django.conf import settings
(Pdb) settings.DATE_INPUT_FORMATS
('%d-%m-%y', '%d-%b-%Y', '%d-%B-%y', '%d-%m-%Y', '%d-%b-%y', '%d-%B-%Y', '%d/%m/%y', '%d/%b/%Y', '%d/%B/%y', '%d/%m/%Y', '%d/%b/%y', '%d/%B/%Y', '%d %m %y', '%d %b %Y', '%d %B %y', '%d %m %Y', '%d %b %y', '%d %B %Y', '%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', '%b %d %Y', '%b %d, %Y', '%d %b %Y', '%d %b, %Y', '%B %d %Y', '%B %d, %Y', '%d %B %Y', '%d %B, %Y', '%x')
(Pdb) getattr(settings, 'DATE_INPUT_FORMATS')
('%d-%m-%y', '%d-%b-%Y', '%d-%B-%y', '%d-%m-%Y', '%d-%b-%y', '%d-%B-%Y', '%d/%m/%y', '%d/%b/%Y', '%d/%B/%y', '%d/%m/%Y', '%d/%b/%y', '%d/%B/%Y', '%d %m %y', '%d %b %Y', '%d %B %y', '%d %m %Y', '%d %b %y', '%d %B %Y', '%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y', '%b %d %Y', '%b %d, %Y', '%d %b %Y', '%d %b, %Y', '%B %d %Y', '%B %d, %Y', '%d %B %Y', '%d %B, %Y', '%x')
(Pdb) formats.get_format('DATE_INPUT_FORMATS')
('%Y-%m-%d', '%m/%d/%Y', '%m/%d/%y')
(Pdb)

Change History (1)

comment:1 by Karen Tracey, 12 years ago

Resolution: invalid
Status: newclosed

What is your USE_L10N setting? As noted in the doc for `DATE_INPUT_FORMATS` it is only used when USE_L10N is False. I suspect in your case it is True and the values being used are the locale-specific ones instead.

Note: See TracTickets for help on using tickets.
Back to Top