-->

“Enter a valid date” Error in Django Forms DateFie

2019-09-07 08:02发布

问题:

I know there are a lot of threads alredy talking about this but there is none that remotely solved my problem. This is really weird because everything has been done how it's meant to be done.

Here is my forms.py:

from django import forms
from airspress import settings

class searchForm(forms.Form):
depDate1 = forms.DateField(required=False,  input_formats=settings.DATE_INPUT_FORMATS)
depDate2 = forms.DateField(required=False, input_formats=settings.DATE_INPUT_FORMATS)
cityDep = forms.CharField(required=False)
cityArr = forms.CharField(required=False)

And here is a simple test :

data = {'depDate1':'1-5-2015'}
boo = searchForm(data)
if boo.is_valid():
   pass
print boo.errors
#print: <ul class='errorlist'><li>depDate1<ul><li>Enter a valid date</li></ul></li></ul>

In short i have an "Enter a valid date" error, And i've tried absolutely everything, every kind of date format i can think of, it only works when i create an actual datetime.datetime object and assign to the field value. I've done lot of testing in actual rendered html form to no avail.

Hope I'm actually missing something and i'm not just doomed for the day...

Thank you very much !

回答1:

Well, i don't know what to say... Maybe Django had a "caching" problem or something like that. Because there don't seem to be a scripting problem which just happen to solve itself (unless there's a 3035 year robot software around). If anyone has this problem, follow the answers on other questions here, like this one :

django Enter a valid date. validation error

Thank you very much for you time @AirThomas .