Reputation: 827
I have a form field that I want to have as a calendar widget that defaults to the current date. I had it so it was showing the date in d/m/y format but when I'd submit it would say 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 %}
Upvotes: 0
Views: 593
Reputation: 91
If you see carefully, your DateInput format is as format=('%d-%m-%Y')
while your question states that your format for date is in d/m/y. Try with the hyphen instead of the slash, or vice versa and you should be fine.
Upvotes: 1