Skip to content

Instantly share code, notes, and snippets.

@niccolomineo
Last active November 28, 2025 11:41
Show Gist options
  • Select an option

  • Save niccolomineo/d61b5dc0591b67361a55b016bb2a7f8c to your computer and use it in GitHub Desktop.

Select an option

Save niccolomineo/d61b5dc0591b67361a55b016bb2a7f8c to your computer and use it in GitHub Desktop.
Django HTML date input
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