I have been looking for ways to set my Django form to only accept dates that are today or days in the future. I currently have a jQuery datepicker on the frontend, but here is the form field to a modelform.
Thanks for the help, much appreciated.
date = forms.DateField(
label=_("What day?"),
widget=forms.TextInput(),
required=True)
You could add a clean()
method in your form to ensure that the date is not in the past.
import datetime
class MyForm(forms.Form):
date = forms.DateField(...)
def clean_date(self):
date = self.cleaned_data['date']
if date < datetime.date.today():
raise forms.ValidationError("The date cannot be in the past!")
return date
See http://docs.djangoproject.com/en/dev/ref/forms/validation/#cleaning-a-specific-field-attribute