The form does not valid because of the date field is not a valid format

Form is not validating because of date field is not a valid format can any one help me.

settings.py

USE_I18N = True

USE_L10N = True

USE_TZ = True

API = 'Apache-HttpClient'

ACCEPTABLE_FORMATS = ['%Y-%m-%d',       # '2006-10-25'
                      '%d-%m-%Y',       # '25-10-2006'
                      '%d/%m/%Y',       # '25/10/2006'
                      '%d/%m/%y']       # '25/10/06'

models.py

class SaveTrip(models.Model):
    """storing user saving trips
    """
    user = models.ForeignKey(User)
    From = models.CharField(max_length=32)
    to = models.CharField(max_length=32)
    from_date = models.DateField(auto_now=False, auto_now_add=False)
    to_date = models.DateField(auto_now=False, auto_now_add=False)
    choice = models.CharField(max_length=32, null=True, blank=True)

    class Meta:
        verbose_name = "User Saved Trips"

    def get_trip(self):
        return "%s -> %s" % (self.From, self.to)

    def __unicode__(self): # __str__ on Python 3
        return self.get_trip()

Forms.py

class SaveTripForm(forms.Form):
    From = forms.CharField(max_length=32)
    to = forms.CharField(max_length=32)
    from_date = forms.DateField(input_formats=ACCEPTABLE_FORMATS)
    to_date = forms.DateField(input_formats=ACCEPTABLE_FORMATS)
    choice = forms.CharField(max_length=32, required=False)

    def clean(self):
        cleaned_data = self.cleaned_data

        From = cleaned_data.get("From")
        to = cleaned_data.get("to")
        from_date = cleaned_data.get("from_date")
        to_date = cleaned_data.get("to_date")
        choice = cleaned_data.get("choice")
        return cleaned_data

shell

python manage.py shell
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> a = {u'From':[u'Bangalore'], u'userid':[u'50'], u'choice':[u'(4,)'], u'to':[u'Goa'], u'from_date':[u'2016-10-20'], u'to_date':[u'2016-10-23']}
>>> from uprofile.forms import SaveTripForm
>>> abc = SaveTripForm(a)
>>> abc.is_valid()
False
>>> abc.errors
{'from_date': [u'Enter a valid date.'], 'to_date': [u'Enter a valid date.']}
>>> abc.cleaned_data
{'to': u"[u'Goa']", 'From': u"[u'Bangalore']", 'choice': u"[u'(4,)']"}
>>>

When you look at a query dict (e.g. request.POST
), you will see lists, because HTML forms can submit multiple values for the same key.

However, when you construct the data dictionary manually, you should use strings instead of lists.

a = {u'From': 'Bangalore', u'userid': '50', u'choice':'(4,)', u'to':'Goa', u'from_date':'2016-10-20', u'to_date':'2016-10-23'}
abc = SaveTripForm(a)
Hello, buddy!责编内容来自:Hello, buddy! (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 综合编程 » The form does not valid because of the date field is not a valid format

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录