1👍
We’re having a similar problem and it’s driving me crazy, I’ve solved it but I can find neither the cause nor a pattern to reproduce the bug.
We have the DATE_INPUT_FORMATS
defined on the settings as well, and the validation is working as expected. Now we’re moving a heavy feature that’s causing Bad Gateways to a celery task, and certain form will be initialized inside the task. The first day of testing everything was working fine, but yesterday we encountered some problems.
We have two DateField
in the form, same format, and sometimes one of them throws a validation error. I can’t find any pattern with the dates; every time I reload the page, select the dates and launch the task, the behaviour seems to be different even selecting the same dates (sometimes it gets validated, sometimes not). If I use pdb
and manually initialize the form before calling the task, it’s validated correctly. I’ve also checked that the form data inside the task is exactly the same, so the problem must be somehow related to Celery
and the DATE_INPUT_FORMATS
setting.
Eventually I solved it by defining the optional input_formats
argument on the problematic field of the form:
class DateFilterForm(forms.Form):
date_from = forms.DateField(
widget=forms.DateInput(
format='%d/%m/%Y', attrs={'class': 'date_input'}
)
)
date_to = forms.DateField(
input_formats = ['%d/%m/%Y'],
widget=forms.DateInput(
format='%d/%m/%Y', attrs={'class': 'date_input'}
)
)
And that way it always works fine, though I haven’t been able to figure out the logic behind it. Our current environment uses Django==1.4.16
and celery==2.5.1
, if anyone is interested (we hope to upgrade that soon, so I’ll check if that solves the original problem).