"输入有效日期"编辑表单时django中的错误

时间:2015-09-23 11:40:03

标签: python django

我在编辑表单时遇到日期问题。

model.py

class Promotions(models.Model):
    name = models.CharField(max_length=100)
    description = models.CharField(max_length=10000)
    status = models.CharField(max_length=100)
    website = models.CharField(max_length=100)
    start_date = models.DateTimeField()
    expire_date = models.DateTimeField()

forms.py

class PromotionsForm(forms.ModelForm):
    CHOICES = (('Active','Active'),('Inactive', 'Inactive'))
    WEBSITE = (("CMS","CMS"),("Tutor","Tutor"),("Offers","Offers"),
               ("Promotions","Promotions"),("Orders","Orders"))
    status = forms.ChoiceField(choices=CHOICES)
    website = forms.MultipleChoiceField(choices=WEBSITE, widget=forms.SelectMultiple)
    start_date = forms.DateField(widget=forms.TextInput(attrs={'class':'datepicker'}))
    expire_date = forms.DateField(widget=forms.TextInput(attrs={'class':'datepicker'}))
    class Meta:
        model = Promotions
        fields = ["name", "description", "status", "website", "start_date", "expire_date"]
当我提供输入时

模板 the date format when i creating new record

我在编辑时

模板

the date format when i am in editing page

请帮忙 谢谢。

1 个答案:

答案 0 :(得分:1)

模型中的字段类型expire_datestart_date与表单中的字段类型不匹配。

值将存储为DateTime而不是Date。

在您的模型中,将start_dateexpiry_date更改为DateField

class Promotions(models.Model):
    name = models.CharField(max_length=100)
    description = models.CharField(max_length=10000)
    status = models.CharField(max_length=100)
    website = models.CharField(max_length=100)
    start_date = models.DateField()
    expire_date = models.DateField()