0

In forms.py line-no.15, there is .clean() and the method name is also clean(). So whats the difference between those 2 clean met

forms.py from django import forms from django.core import validators class StudentRegister(forms.Form): name=forms.CharField() rollno=forms.IntegerField() email=forms.EmailField() password=forms.CharField(widget=forms.PasswordInput) rpassword=forms.CharField(widget=forms.PasswordInput) feedback=forms.CharField(widget=forms.Textarea, validators=[validators.MaxLengthValidator(40), validators.MinLengthValidator(10)]) def clean(self): print('after user has submitted the form!') print('total form validation!! Im in StudentRegister class') d = super().clean() print(d) inputpwd = d['password'] inputrpwd = d['rpassword'] if inputpwd != inputrpwd: raise forms.ValidationError('Password does not match') inputname = d['name'] if len(inputname)>10: raise forms.ValidationError('Please enter a shorter name') inputemail = d['email'] if not inputemail.endswith('@gmail.com'): raise forms.ValidationError("Please enter a valid email") views.py from django.shortcuts import render from formapp import forms def register(request): if request.method=='GET': form = forms.StudentRegister() # form object has html code. if request.method=='POST': form=forms.StudentRegister(request.POST) if form.is_valid(): print('form validation success!!') print(form.cleaned_data['name']) print(form.cleaned_data['rollno']) print(form.cleaned_data['email']) print(form.cleaned_data['password']) print(form.cleaned_data['rpassword']) return render(request, 'formapp/register.html', {'form':form}) And one more query, after I give the submit button in the form, all the data entered in the form stays but the password field goes off. Why is that ?

20th Aug 2022, 7:48 AM
Levi
Levi - avatar
1 Antwort
+ 3
it takes the method from the super class. You have to find out what form.Forms.clean() does
20th Aug 2022, 9:11 AM
Slick
Slick - avatar