I am on Django 4.0.x and running over and over into the same issue when validating a form.
Always seeing: 'SignupForm' object has no attribute 'instance'
Here is my view:
def signup(request):
if request.method == "POST":
signup_form = SignupForm(request.POST)
if signup_form.is_valid():
email = signup_form.cleaned_data['email']
else:
signup_form = SignupForm()
return render(request, 'signup.html', {'signup_form': signup_form})
and the according forms.py:
class SignupForm(forms.Form):
email = forms.EmailField(label=_("E-Mail Address"), validators=[EmailValidator()])
password = forms.CharField(label=_("Password"), widget=forms.PasswordInput())
password_confirm = forms.CharField(label=_("Confirm Password"), widget=forms.PasswordInput())
def clean_email(self):
data = self.cleaned_data['email']
if forms.ValidationError:
self.add_error('email', forms.ValidationError)
return data
def clean_password(self):
data = self.cleaned_data['password']
try:
validate_password(data, self.instance)
except forms.ValidationError as error:
self.add_error('password', error)
return data
cleaned_data also delivers nothing here.
CodePudding user response:
I don't know what your validate_password
method does, however your problem seems to be coming from the self.instance
you're passing as one of its parameters. Using self
instead of self.instance
would probably solve your problem.