Open GCru opened 5 years ago
I encounter this bug too. It seems not working correctly when maxDate is the same day as the actual day. I get around the problem by add 1 to it:
# forms.py
from django import forms
from django.utils import timezone
from dateutil.relativedelta import relativedelta
from .models import SomeModel
from bootstrap_datepicker_plus import DatePickerInput
class SomeModelForm(forms.ModelForm):
class Meta:
model = SomeModel
fields = [
'date'
]
def __init__(self, *args, **kwargs):
super(SomeModelForm, self).__init__(*args, **kwargs)
self.fields['date'].widget = DatePickerInput(
options={
"maxDate": (
timezone.localdate() + relativedelta(days=1)
).strftime('%Y-%m-%d')
},
)
Also I found this today in another issue How to restrict date and time in django bootstrap datetimepicker plus?
Using strftime('%Y-%m-%d 23:59:59')
instead strftime('%Y-%m-%d')
solves the issue too.
Comment for guys who faced with same issue, but with the minDate
.
You need to set initial date value for datepicker this way.
self.fields['min_date_field'].widget = DatePickerInput(
format='%Y-%m-%d', options={'minDate': '2021-01-01', "date": '2021-04-04', })
Thank you for the great widget.
I am using the minDate and maxDate options in the widgets. The minDate option works as expected. The maxDate option correctly sets the max date, but then also populates the date field as the initial value or default value every time the form is called, instead of using the value from the initial dictionary passed from Views. When only using the minDate option, the correct initial value from the initial dictionary is set.
I also tried another option by not passing the initial dictionary from Views, but setting the defaultDate under options. As before, this works fine alongside the minDate option but gets overridden by the maxDate option so that the default date shown is maxDate.
See my script below. The initial (default) value of date_y is always set to maxDate. Note that in both cases I am defining the widgets in form
__init__
because I am passing the minDate and maxDate from Views as these will change each time the form is called.Thank you Gerhard