widgets.py Downloaded from http://djangosnippets.org/snippets/1688/ Changes: added the attribute 'value' into the class consructor to pass initial value Changed on: 18.04.2011 Changed by: Alexandr Seleznev
import datetime
import re
from django.forms.widgets import Select
from django.forms.widgets import Widget
from django.utils.dates import MONTHS
from django.utils.safestring import mark_safe
__all__ = ('SelectDateWidget',)
RE_DATE = re.compile(r'(\d{4})-(\d\d?)-(\d\d?)$')
A Widget that splits date input into three
This also serves as an example of a Widget that has more than one HTML element and hence implements value_from_datadict.
class SelectDateWidget(Widget):
years is an optional list/tuple of years to use in the "year" select box.
self.attrs = attrs or {}
self.required = required
self.value = value
if years:
self.years = years
else:
this_year = datetime.date.today().year
self.years = range(this_year, this_year + 10)