Skip to content

Instantly share code, notes, and snippets.

@niccolomineo
Last active October 21, 2025 16:52
Show Gist options
  • Save niccolomineo/d61b5dc0591b67361a55b016bb2a7f8c to your computer and use it in GitHub Desktop.
Save niccolomineo/d61b5dc0591b67361a55b016bb2a7f8c to your computer and use it in GitHub Desktop.
A modern Django HTML date input
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