[Answered ]-Name 'user' is not defined, django forms

1πŸ‘

βœ…

The error user is not defined makes complete sense since user is not defined anywhere.

To resolve the problem, try to use self.instance.mnemonic which will give the current instance’s mnemonic so:

class UserUpdateForm(forms.ModelForm):
    class Meta:
        model = User
        fields = ['image', 'mnemonic']

    def clean(self):
        super(UserUpdateForm, self).clean()
        mnemonic = self.cleaned_data.get('mnemonic')
        if mnemonic != self.instance.mnemonic:
            self._errors['mnemonic'] = self.error_class([
                'The mnemonic is wrong!'])
        return self.cleaned_data

Note: Function based views are generally written in snake_case not PascalCase unlike class based views, so it is better to rename it as profile_view from ProfileView.

Leave a comment