我有一个想要作为日历小部件的表单域,默认为当前日期。我有它,所以它以d / m / y格式显示日期,但是当我提交时会显示Enter a valid date
forms.py
class CreateBlogPostForm(forms.ModelForm):
published = forms.DateField()
class Meta:
model = BlogPost
fields = ('title', 'published','featured_image', 'post',)
widgets = {
'title': forms.TextInput(attrs={'class': 'blog-title-field', 'placeholder': 'Title'}),
'published': forms.DateInput(format=('%d-%m-%Y'), attrs={"type": 'date'}),
'post': forms.TextInput(attrs={'class': 'blog-post-field', 'placeholder': 'Write something..'}),
}
models.py
class BlogPost(models.Model):
title = models.CharField(max_length=100)
published = models.DateField()
featured_image = models.ImageField(upload_to='blog/%Y/%m/%d')
post = models.TextField()
slug = AutoSlugField(null=True, default=None,
unique=True, populate_from='title')
class Meta:
verbose_name_plural = "Blog"
def __str__(self):
return self.title
create-blog.html
{% extends 'base.html' %}
{% block content %}
<div class="container text-center">
<form enctype="multipart/form-data" method="POST">
{% csrf_token %}
{{form.title}}
{{form.post}}
{{form.featured_image}}
{{form.published}}
{{form.errors}}
<button type="submit" class="btn btn-primary"><i class="fa fa-plus" aria-hidden="true"></i> Submit</button>
</form>
</div>
{% endblock content %}
答案 0 :(得分:1)
如果您仔细查看,则您的DateInput格式为format=('%d-%m-%Y')
,而您的问题指出日期格式为d / m / y。尝试使用连字符而不是斜杠,反之亦然,应该没事。