[Django]-Can I limit the dates possibilities using django widget DateInput, ex. between 2020-01-01 and 2023-12-31

5👍

Yes it’s possible. Have a look at the docs here and the example below.

class CommentForm(forms.Form):
    name = forms.DateField(widget=forms.DateField(attrs={'min': '2020-01-01', 'max': '2021-01-01'}))

Or in the __init__:

self.fields['name'].widget.attrs.update({'min': '2020-01-01', 'max': '2021-01-01'})

You might want to use a datepicker made by someone else since the HTML5 datepicker might not show on IE or Safari.

Leave a comment