【问题标题】:Django change password issue, super(type, obj): obj must be an instance or subtype of typeDjango更改密码问题,super(type, obj): obj must be an instance or subtype of type
【发布时间】:2014-08-29 07:02:53
【问题描述】:

我的 changepassword 表单有问题,它继续给我同样的错误:super(type, obj): obj must be an instance or subtype of type

这是我的表格:

class PasswordChangeForm(forms.Form):
    current_password = forms.CharField(label=u'Current Password', widget=forms.PasswordInput(render_value=False))
    new_password = forms.CharField(label=u'New Password', widget=forms.PasswordInput(render_value=False))
    retyped_password = forms.CharField(label=u'Retype New Password', widget=forms.PasswordInput(render_value=False))

    def __init__(self, data=None, user=None, *args, **kwargs):
        self.user = user
        super(UserProfileEditForm, self).__init__(data=data, *args, **kwargs)

    def clean_current_password(self):
        cleaned_data = self.cleaned_data
        current_password = cleaned_data.get('current_password', '')

        if not self.user.check_password(current_password):
            raise ValidationError('Wrong current password.')

        return current_password

    def clean(self):
        cleaned_data = self.cleaned_data
        new_password = cleaned_data.get('new_password', '')
        retyped_password = cleaned_data.get('retyped_password', '')

        if len(new_password) == 0 or len(retyped_password) == 0:
            raise ValidationError('Blank password fields.')

        if new_password != retyped_password:
            raise ValidationError('New password and retyped password do not match.')

        return cleaned_data

    def save(self):
        self.user.set_password(new_password)
        return self.user

有什么想法吗?

【问题讨论】:

    标签: python django passwords


    【解决方案1】:

    您的问题在这一行:

    super(UserProfileEditForm, self).__init__(data=data, *args, **kwargs)
    

    应该是

    super(PasswordChangeForm, self).__init__(data=data, *args, **kwargs)
    

    当您从其他表单复制时,这可能是复制粘贴问题。

    更多context here

    【讨论】:

    • 谢谢你,我还有一个小问题......非形式没关系,但是当我调用保存函数时,它说全局 new_passord 它没有定义
    • 你需要做new_password = self.cleaned_data.get('new_password')
    猜你喜欢
    • 1970-01-01
    • 2019-01-05
    • 1970-01-01
    • 2021-04-14
    • 2021-05-12
    • 2021-11-23
    • 1970-01-01
    • 2017-08-14
    • 2018-03-31
    相关资源
    最近更新 更多