0

in the model, I have two fields like

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

in the forms i have defined it as

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

in the base html, I have written the jQuery code for datepicker as

<script>

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

    </script>

I have zero knowledge in javascript and jQuery, all I want to implement is to_date should be always greater than from_date. How do I do that?

my model

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

my forms.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()

    }
  • i think you should try to find the solution in the internet, try it and if you get troubles do new research and only after it, create new question. – Brown Bear Sep 07 '18 at 09:11

2 Answers2

1

Try This,

 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
    }

For More details refer Date Compare

Som
  • 598
  • 3
  • 12
1

Create the Django Models as below

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

Write the Form for above model as below

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

With above model and form you don't have to do any modification with HTML