Skip to content

Instantly share code, notes, and snippets.

@julianwachholz
Created September 26, 2013 19:21
Show Gist options
  • Save julianwachholz/6719216 to your computer and use it in GitHub Desktop.
Save julianwachholz/6719216 to your computer and use it in GitHub Desktop.
Easy single `<input>` month/year form field for Django.
class CreditCardExpirationField(forms.DateField):
default_error_messages = {
'min_value': _("This card has expired."),
'invalid': _("Please specify a valid expiration date.")
}
widget = forms.DateInput(format='%m/%Y')
input_formats = ('%m/%Y', '%m/%y')
default_validators = [
MinValueValidator(now().date()),
]
def to_python(self, value):
"""
Set day to last day of month.
Credit cards are valid through the last day of the specified month.
"""
value = super(CreditCardExpirationField, self).to_python(value)
last_day = monthrange(value.year, value.month)[1]
return datetime.date(value.year, value.month, last_day)
@Allan-Nava
Copy link

There is a fields for month field?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment