问题
I have a problem with date format. I'm using 'Y-m-d' format. When I enter the format the next time I want to edit, swaps month and day. Reading the doc I see input_format parameter, so I force it input_formats='%Y-%m-%d'
. And I get an error that the date is not valid.
forms.py:
date_start = forms.DateField(input_formats='%Y-%m-%d',widget=forms.DateInput(format = '%Y-%m-%d',attrs={'class':'datepicker form-control'}))
template:
<div class="row row-padded">
<div class="col-md-6">
<label>
{{ form.date_start.label }}
</label>
</div>
<div class="col-md-6 {%if form.date_start.errors %}has-error{%endif%}">
{{ form.date_start }}
</div>
</div>
<script>
$(function() {
$( ".datepicker" ).datepicker();
$( ".datepicker" ).datepicker("option", "dateFormat","yy-mm-dd");
});
</script>
models.py:
date_start = models.DateField()
SOLVED: No need to force date_format in django. The problem was the picker
forms.py
date_start = forms.DateField(widget=forms.DateInput(attrs={'class':'datepicker form-control'}))
template
<script>
$(function() {
$('.datepicker').datepicker({ dateFormat: 'yy-mm-dd' });
});
</script>
回答1:
In your form your date format is %Y-%m-%d
=> yyyy-mm-dd
which is not equal to the format you set within your date picker script!
来源:https://stackoverflow.com/questions/19862519/django-date-format-swap