Django date format swap

二次信任 提交于 2019-12-11 05:25:52

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!