1

I am trying to use the SplitDateTimeWidget but want it to accept date in day - month - year format.

from django.forms.widgets import SplitDateTimeWidget

class EventForm(forms.ModelForm):
    class Meta:
        model = Event
        widgets = {'start': SplitDateTimeWidget(date_format='%d/%m/%Y')}

The SplitDateTimeWidget accepts a date_format argument, which I expect to be used to validate the date input but it isn't.

The default widget is correctly replaced but it ignores the date_format and insists on validating against the default month - day - year.

I also tried setting the DATE_FORMAT and DATE_INPUT_FORMATS settings with no luck.

Thanks for any help.

onurmatik
  • 5,105
  • 7
  • 42
  • 67

4 Answers4

7

This worked for me:

class EventForm(forms.ModelForm):

    start = SplitDateTimeField(input_date_formats=['%d/%m/%Y'],
                               input_time_formats=['%H:%M'], 
                               widget=SplitDateTimeWidget(date_format='%d/%m/%Y',
                                                          time_format='%H:%M'),
                               )

    class Meta:
        model = Event
Kerridge0
  • 2,017
  • 19
  • 22
1

I had the same problem, trying to make the date part of SplitDateTimeField accept dates in the format '%d/%m/%Y'.

The solution above by Marat did not work for me(including the correction to it by omat)

I have finally solved the problem by overriding the default list of datetime input formats in settings.py:

DATETIME_INPUT_FORMATS = ('%d/%m/%Y %I:%M', '%Y-%m-%d %H:%M', '%Y-%m-%d',
    '%m/%d/%Y %H:%M:%S', '%m/%d/%Y %H:%M', '%m/%d/%Y',
    '%m/%d/%y %H:%M:%S', '%m/%d/%y %H:%M', '%m/%d/%y')

I added the desired format as the first in the list so it will take precedence over the others.

From Django documentation: "Formats will be tried in order, using the first valid"

amir_
  • 11
  • 1
1

Widget date format responsible only for output and does not matter for validation. What matters is field type, and in case of SplitDateTimeField it uses DateField and TimeField which are instantiated using input_date_formats parameter.

So the answer is:

class EventForm(forms.ModelForm):
    class Meta:
        model = Event
        widgets = {'start': SplitDateTimeWidget(date_format='%d/%m/%Y')}

    start = SplitDateTimeField(input_date_formats='d/m/Y',
                               input_time_formats='<whatever, or skip it>')

Note that input_date_formats is a Django format, http://docs.djangoproject.com/en/dev/ref/templates/builtins/#date

Marat
  • 15,215
  • 2
  • 39
  • 48
  • thanks, it worked. one correction: `input_date_formats` should be `['%d/%m/%Y']` – onurmatik Oct 06 '11 at 15:14
  • `date_format` is ignored when displaying a value in the field while editing. although `SplitDateTimeWidget(date_format='%d/%m/%Y')` the value is displayed in the field as `YYYY-MM-DD`. the `DATE_FORMAT` setting is also ignored. :( – onurmatik Oct 07 '11 at 10:37
0

Apparently SplitDateTimeWidget argument date_format MUST be '%Y-%m-%d' otherwise it won't work as expected, as the following example:

start=forms.SplitDateTimeField(
    label=_('Start'),
    widget=forms.SplitDateTimeWidget(
    date_attrs={
        'type':'date'
        },
    date_format='%Y-%m-%d',
    time_attrs={
        'type':'time'
        },
    time_format='%H:%M',
    )
)
obarbachan
  • 61
  • 5