[Django]-How to fix “TypeError: string indices must be integers” when using cleaned_data?

3👍

Clean should return dict. You need to rewrite clean method:

def clean(self):
        cleaned_data = super(ResetActivatioKey, self).clean()
        try:
            user = User.objects.get(email__iexact=self.cleaned_data['email'])
            return cleaned_data
        except:
            raise forms.ValidationError('User with this email does not exist!')    

3👍

Your clean() method is returning a string with the email you just entered in it. You may need to call super() on your method to get the cleaned_data as dict and return the same to the view.

You need to edit your clean() method in your form,

def clean(self):
    cleaned_data = super(ResetActvatioKey, self).clean()
    try:
        user = User.objects.get(email__iexact=self.cleaned_data['email'])
        return cleaned_data
    except:
        raise forms.ValidationError('User with that email does not exist!')

Leave a comment