Last active
October 21, 2025 16:52
-
-
Save niccolomineo/d61b5dc0591b67361a55b016bb2a7f8c to your computer and use it in GitHub Desktop.
A modern 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 datetime import date | |
| from django.forms.widgets import DateInput as DjangoDateInput | |
| class DateInput(DjangoDateInput): | |
| """Custom select date widget.""" | |
| input_type = "date" | |
| offset_years = 5 | |
| def __init__(self, attrs=None, *args, **kwargs): | |
| """Initialize widget.""" | |
| attrs = attrs or {} | |
| year_now = date().today().year | |
| attrs["min"] = date(year_now - self.offset_years, 1, 1).strftime("%Y-%m-%d") | |
| attrs["max"] = date(year_now + 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