1

I have n number of dates defined in this below model in the database, these are directly inserted by admin, not by modelforms or anything.

class Calendar(models.Model):

    date = models.DateField()
    occasion = models.CharField(max_length = 20)

the form that has the date fields,

class LeaveRequestForm(ModelForm):

    class Meta:
        fields = (... "from_date", "to_date" ...)
        model = Leave

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

        }

I have this datepicker script which disable weekends,

<script>

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

    </script>

Now I want to disable the dates defined in the database + weekends as well. How do I do that?

update :

var array = {{ 
               {% for x in disabled_dates %}
               {{x.dates}}
               {% endfor  %} 
            }}

$('datepicker').datepicker({
    beforeShowDay: function(date){
        var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
        return array.indexOf(string) != -1 ? [false] : $.datepicker.noWeekends(date);
    }
});

No luck yet!

Aasma Rehman
  • 13
  • 1
  • 4

1 Answers1

2

As referenced from the answers here and here,

var array = [
    {% for x in disabled_dates %}
        {{x.dates}}
    {% endfor  %} 
]

$('datepicker').datepicker({
    beforeShowDay: function(date){
        var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
        return array.indexOf(string) != -1 ? [false] : $.datepicker.noWeekends(date);
    }
});

Edit: Added the weekends alongside your array of dates.

Edit: Added for loop for dates (although i think you should've sent a ready list from your view function)

Hope this helps!

devdob
  • 1,404
  • 12
  • 13