Django:ModelForm中的日期选择器

时间:2018-09-07 08:04:02

标签: javascript python jquery django

在模型中,我有两个字段,例如

from_date = models.DateField()
to_date = models.DateField()

以我将其定义为的形式

widgets =  {
            'from_date' : DateInput(attrs={'class': 'datepicker'}),
            'to_date' : DateInput(attrs={'class': 'datepicker'}),
           }

在基本html中,我为datepicker编写了jQuery代码,

<script>

    $(document).ready(function() {
        $('.datepicker').datepicker(
          {
            minDate : 0,
            beforeShowDay : $.datepicker.noWeekends
          }
        );
    });

    </script>

我对javascript和jQuery的知识为零,我想要实现的是 to_date应始终大于from_date。我该怎么办?

我的模特

class Leave(models.Model):

    employee_ID = models.CharField(max_length = 20)
    name = models.CharField(max_length = 50)
    user = models.ForeignKey(User, on_delete = models.CASCADE, null =True)
    department = models.CharField(max_length = 50, choices = DEPARTMENT_CHOICES)
    designation = models.CharField(max_length = 50, choices = DESIGNATION_CHOICES)
    type_of_leave = models.CharField(max_length = 15, choices = CHOICES)
    from_date = models.DateField()
    to_date = models.DateField()
    reporting_manager = models.CharField(max_length = 50, choices = MANAGER_CHOICES)
    reason = models.CharField(max_length= 180)
    status = models.CharField(max_length = 15, choices = STATUS_CHOICES)
    reason_reject = models.CharField(('reason for rejection'),max_length=50, default = '-') #blank=True)
    created = models.DateField(auto_now_add=True, editable=False, null=False, blank=False)
    last_modified = models.DateField(auto_now=True, editable=False, null=False, blank=False)


    def __str__(self):
        return self.name

我的表格.py

class LeaveRequestForm(ModelForm):
class Meta:
    fields = ("name", "employee_ID" ,"department", "designation", "type_of_leave", "from_date", "to_date", "reporting_manager", "reason")
    model = Leave

    widgets =  {
        'name': Textarea(attrs = {'cols' : 20, 'rows': 1}),
        'employee_ID' : Textarea(attrs = {'cols' : 20, 'rows': 1}),
        'from_date' : DateInput(attrs={'class': 'datepicker'}),
        'to_date' : DateInput(attrs={'class': 'datepicker'}),
        'reason_reject' : forms.HiddenInput()

    }

2 个答案:

答案 0 :(得分:1)

尝试一下,

 var from_date = models.DateField();//Your From Date
    var to_date = models.DateField();//Your To Date

    if(new Date(to_date) > new Date(from_date))//This Condition Compares both Dates 
    {
         //Your Code
    }

有关更多详细信息,请参见Date Compare

答案 1 :(得分:1)

按如下所示创建Django模型

class Request(models.Model):
    from_date = models.DateField(default = timezone.now, blank = True)
    to_date = models.DateField(default = timezone.now, blank = True)

如下所示为上述模型编写表格

class TimestampForm (forms.Form):
    def clean(self):
        cleaned_data = super(TimestampForm, self).clean()
        from_date = cleaned_data.get("from_date")
        to_date = cleaned_data.get("to_date")
        if from_date > to_date:
            raise forms.ValidationError("'From date' cannot be a later time than 'To date'.")
        if to_date > (timezone.now()):
            raise forms.ValidationError("'To date' cannot be a future time.")
        return cleaned_data
    from_date = forms.DateField(label = 'From Date * ', widget = DateWidget(attrs = {'id':"from_date"}, usel10n = True, bootstrap_version = 3))
    to_date = forms.DateField(label = 'To Date', widget = DateWidget(attrs = {'id':"to_date"}, usel10n = True, bootstrap_version = 3))

使用上述模型和表格,您无需对HTML进行任何修改