django- jquery 日期时间选择器无法保存选定日期的表单
django- jquery Datetime picker cannot save the form with date selected
在 html 它显示了一个表单,如果我没有 select 来自日期时间选择器的任何日期,表单可以成功提交。
但是一旦我 select 编辑了一个日期,我就无法提交了。所以我猜日期时间格式可能有问题。
日期时间选择器在 html 中工作,它显示下拉列表,我也可以 select 它。只是如果我 select 约会,我无法提交表格。
形成日期时间选择器的 html 片段
$(function() {
$('.date-picker').datepicker(
{
dateFormat: "yymm",
changeMonth: true,
changeYear: true,
showButtonPanel: true,
onClose: function(dateText, inst) {
function isDonePressed(){
return ($('#ui-datepicker-div').html().indexOf('ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all ui-state-hover') > -1);
}
if (isDonePressed()){
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$(this).datepicker('setDate', new Date(year, month, 1)).trigger('change');
$('.date-picker').focusout()//Added to remove focus from datepicker input box on selecting date
}
},
beforeShow : function(input, inst) {
inst.dpDiv.addClass('month_year_datepicker')
if ((datestr = $(this).val()).length > 0) {
year = datestr.substring(datestr.length-4, datestr.length);
month = datestr.substring(0, 2);
$(this).datepicker('option', 'defaultDate', new Date(year, month-1, 1));
$(this).datepicker('setDate', new Date(year, month-1, 1));
$(".ui-datepicker-calendar").hide();
}
}
})
});
models.py
start_date=models.DateField(auto_now=False, auto_now_add=False)
end_date=models.DateField(auto_now=False, auto_now_add=False)
forms.py
widgets = {
'start_date': forms.DateInput(attrs={'class':'datepicker'}),
'end_date': forms.DateInput(attrs={'class':'datepicker'}),
}
在 html 中,日期格式如 201010 (YYYYMM)。
我在想是不是因为html里面的日期格式和模型里的不一样,所以没能提交。
那请问遇到这种情况如何修改模型或表格?日期格式应为 html (YYYYMM) 201003 等
您需要配置表单中的表单字段 class 以接受您选择的日期格式:
start_date = forms.DateField(widget=DateInput(format='%Y%m'), input_formats=['%Y%m'])
end_date = forms.DateField(widget=DateInput(format='%Y%m'), input_formats=['%Y%m'])
(这应该适用于常规 Form
和 ModelForm
)。
在 html 它显示了一个表单,如果我没有 select 来自日期时间选择器的任何日期,表单可以成功提交。
但是一旦我 select 编辑了一个日期,我就无法提交了。所以我猜日期时间格式可能有问题。
日期时间选择器在 html 中工作,它显示下拉列表,我也可以 select 它。只是如果我 select 约会,我无法提交表格。
形成日期时间选择器的 html 片段
$(function() {
$('.date-picker').datepicker(
{
dateFormat: "yymm",
changeMonth: true,
changeYear: true,
showButtonPanel: true,
onClose: function(dateText, inst) {
function isDonePressed(){
return ($('#ui-datepicker-div').html().indexOf('ui-datepicker-close ui-state-default ui-priority-primary ui-corner-all ui-state-hover') > -1);
}
if (isDonePressed()){
var month = $("#ui-datepicker-div .ui-datepicker-month :selected").val();
var year = $("#ui-datepicker-div .ui-datepicker-year :selected").val();
$(this).datepicker('setDate', new Date(year, month, 1)).trigger('change');
$('.date-picker').focusout()//Added to remove focus from datepicker input box on selecting date
}
},
beforeShow : function(input, inst) {
inst.dpDiv.addClass('month_year_datepicker')
if ((datestr = $(this).val()).length > 0) {
year = datestr.substring(datestr.length-4, datestr.length);
month = datestr.substring(0, 2);
$(this).datepicker('option', 'defaultDate', new Date(year, month-1, 1));
$(this).datepicker('setDate', new Date(year, month-1, 1));
$(".ui-datepicker-calendar").hide();
}
}
})
});
models.py
start_date=models.DateField(auto_now=False, auto_now_add=False)
end_date=models.DateField(auto_now=False, auto_now_add=False)
forms.py
widgets = {
'start_date': forms.DateInput(attrs={'class':'datepicker'}),
'end_date': forms.DateInput(attrs={'class':'datepicker'}),
}
在 html 中,日期格式如 201010 (YYYYMM)。
我在想是不是因为html里面的日期格式和模型里的不一样,所以没能提交。
那请问遇到这种情况如何修改模型或表格?日期格式应为 html (YYYYMM) 201003 等
您需要配置表单中的表单字段 class 以接受您选择的日期格式:
start_date = forms.DateField(widget=DateInput(format='%Y%m'), input_formats=['%Y%m'])
end_date = forms.DateField(widget=DateInput(format='%Y%m'), input_formats=['%Y%m'])
(这应该适用于常规 Form
和 ModelForm
)。