Reputation: 3943
I'm using Django and the AdminDateWidget in my form. It works fine. But now I've to level up my form.
I need to insert only a range of date with this widget. The range would be limited in the future, this mean I have to remove the possibility of insert a date after today.
Is it possible? This is my form's code. (it's a dynamic form for having always a refreshed date)
class MyForm(forms.Form):
def __init__(self, name, *args, **kwargs):
super(FirstStartMiceForm, self).__init__(*args, **kwargs)
self.fields['insertion_date'] = forms.DateField(widget = widgets.AdminDateWidget(), initial=date.today())
...
Any ideas?
Upvotes: 1
Views: 1965
Reputation: 985
You can read about form validations here:
https://docs.djangoproject.com/en/dev/ref/forms/validation/
Here is a suggestion:
class MyForm(forms.Form):
def clean_insertion_date(self):
if self.cleaned_data['insertion_date'] > date.today():
raise forms.ValidationError("Time travel are we?")
Upvotes: 2