django- jquery日期选择器无法保存选择日期的表单 [英] django- jquery Datetime picker cannot save the form with date selected

查看:1598
本文介绍了django- jquery日期选择器无法保存选择日期的表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在html中,它显示一个表单,如果我没有从datetime选择器中选择任何日期,表单可能会成功提交。

In the html it displays a form, if I don't select any date from the datetime picker, the form could be submitted successfully.

但是一旦我选择了一个日期然后我不能提交。所以我想这可能会在日期时间格式出错。

But once I selected a date then I couldn't submit it. So I guess there might be something wrong in the date time format.

日期时间选择器在html中工作,它显示下拉列表,我也可以选择它。如果我选择了一个日期,我不能提交表单。

The Datetime picker works in the html, it shows dropdown list and I could also select it. Just I couldn't submit the form if I select a date.

日期时间选择器的表单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)。

In the html, the date format is like 201010 (YYYYMM).

我想知道是不是因为html中的日期格式与模型不一样,这就是为什么我无法提交

I was wondering if it was because the date format in html is not the same as in models, that's why I couldn't submit it.

如果在这种情况下,请告诉我如何更正我的模型或表格?日期格式应该像html(YYYYMM)201003等。

Then please tell me how could I correct my models or forms if in this situation? The date format should be like in html (YYYYMM) 201003, etc.

推荐答案

您需要配置表单中的表单域类接受您选择的日期格式:

You need to configure the form fields in your form class to accept your chosen date format:

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'])

(这应该在常规的表单和一个 ModelForm )。

(This should work in both a regular Form and a ModelForm).

这篇关于django- jquery日期选择器无法保存选择日期的表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆