表格未验证,因为日期字段不是有效格式

时间:2016-04-18 09:43:18

标签: python django django-forms

表单未验证,因为日期字段不是有效格式,任何人都可以帮助我。

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

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,)']"}
>>>

2 个答案:

答案 0 :(得分:0)

当您查看查询字典(例如request.POST)时,您会看到列表,因为HTML表单可以为同一个键提交多个值。

但是,手动构建数据字典时,应使用字符串而不是列表。

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)

答案 1 :(得分:0)

实际上是request.POST数据的问题。 表格未验证,因为帖子数据值在LIST中。 对于这个问题,我们可以这样做 abc = SaveTripForm(a.dict())

壳牌

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.dict())
>>> abc.is_valid()
True
>>> abc.errors
{}
>>>