I added this validation in my froms.py for prevent to accept future date. But I am not undersating why it's not working and forms still now submitting with future date. here is my code:
import datetime
class AddPatientFrom(forms.ModelForm):
date_of_birth = forms.DateField(widget=forms.DateInput(attrs={'class': 'form-control','type':'date'}),required=True)
class Meta:
model = Patient
fields = ['date_of_birth']
def clean_date(self):
date = self.cleaned_data['date_of_birth']
if date < datetime.date.today():
raise forms.ValidationError("The date cannot be in the past!")
return date
I also want to know how to disable to pick future date from Django default html calendar?
CodePudding user response:
You are checking the opposite: it will rase an error if the date of birth is before today. You should check if the date of birth is after today when raising a validation error, so:
def clean_date_of_birth(self):
date = self.cleaned_data['date_of_birth']
if date > datetime.date.today(): #