Last active
November 28, 2025 11:41
-
-
Save niccolomineo/d61b5dc0591b67361a55b016bb2a7f8c to your computer and use it in GitHub Desktop.
Django HTML date input
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from django.forms.widgets import DateInput as DjangoDateInput | |
| from django.utils.timezone import now | |
| class DateInput(DjangoDateInput): | |
| """Custom select date widget.""" | |
| input_type = "date" | |
| year_start = 2020 | |
| offset_years = 5 | |
| def __init__(self, attrs=None, *args, **kwargs): | |
| """Initialize widget.""" | |
| attrs = attrs or {} | |
| attrs["min"] = date(self.year_start, 1, 1).strftime("%Y-%m-%d") | |
| attrs["max"] = date(now().year + self.offset_years, 12, 31).strftime( | |
| "%Y-%m-%d" | |
| ) | |
| super().__init__(attrs, *args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment