python - 日期时间选择器 : how to set the min/max date based on SQL database

标签 python mysql django datetimepicker

有两列:年份;一个 MySQL 表中的月份。 现在我已经实现了在html中显示下拉日期表(年/月)的功能。 (基于 dJango + jquery 日期时间选择器)。我在下面附上了这部分代码;

下一步是根据 SQL 表中的最早/最晚日期 (Y/M) 限制日期范围。然后我不知道如何根据SQL数据库设置这个mindate和maxdate。非常感谢任何指导。

HTML

<!DOCTYPE html>  
<html> 
<head>  
    <meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>  
    <title>Input Table</title>  
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.js"></script>
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js"></script>
    <link rel="stylesheet" type="text/css" media="screen" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/themes/base/jquery-ui.css">

 <script type="text/javascript">
    $(function() {
 $('.date-picker').datepicker(
                {
                    dateFormat: "mm/yy",
                    changeMonth: true,
                    changeYear: true,
                    showButtonPanel: true,
                    mindate:  ***-------How to write the code here to get SQL date range?***


                    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();
                        }
                    }
                })
});
</script>

form.py

class inputform(forms.ModelForm):
    company=forms.CharField() 
    start_date=forms.DateField(widget=DateInput())

class Meta:
    model = input
    fields = ('company','start_date')
    widgets = {
        'start_date': forms.DateInput(attrs={'class':'datepicker'}),
    }

views.py

@csrf_exempt
def input(request):
    if request.method == 'POST': 
        form = inputform(request.POST) 
        if form.is_valid(): 

            start_date=form.cleaned_data('start_date')
            return HttpResponseRedirect('templates/About') 

else:
    form = inputform() 

return render_to_response('inputform.html',{'form': form})

模型.py

from datetime import date
from django.forms import widgets

class input(models.Model):
    company=models.CharField(max_length=100)
    start_date=models.DateField(auto_now=False, auto_now_add=False)

最佳答案

您可以做的是通过数据属性将最小/最大日期范围添加到您的 start_date 字段,如下所示

widgets = {
    'start_date': forms.DateInput(attrs={
        'class':'datepicker', 'data-min': YOUR_MIN_DATE, 
        'data-max': YOUR_MAX_DATE}),
}

然后在日期选择器初始化脚本中使用此属性,如下所示

minDate: $(this).data('min'),
maxDate: $(this).data('max'),

关于python - 日期时间选择器 : how to set the min/max date based on SQL database,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33074842/

相关文章:

python - 基于 numpy 的计算的低效多处理

mysql - 如何让我的 GTFS 查询运行得更快?

mysql - 优化查询或建议 LINQ 等价物

python - Django继承: how to have one method for all subclasses?

Django - 覆盖管理站点的登录表单

django - 使用pycharm在docker中运行django

python - 如何将刷新 token 发布到 Flask JWT Extended?

python - ActivePython 和 Python 有什么区别?

python - python旧版本用法

MySQL - 根据条件获取每个成员的最后记录