【问题标题】:'super' object has no attribute 'clean_password1'“超级”对象没有属性“clean_password1”
【发布时间】:2016-07-09 12:36:12
【问题描述】:

我正在尝试重复使用旧项目中的注册表单代码。问题是 Django 说 UserCreationForm 没有属性 clean_password1

你知道问题出在哪里吗?我看到UserCreationForm 中没有这样的属性,但它以前有效。

我应该怎么做才能让它工作?

编辑:这是因为它调用了 super(...).clean_password1 ,它不在超类中。所以我试图把 super(...).cleaned_data.get("password1") 放在那里,但它引发了 clean_data 不存在的错误(在超类中)。

class UserProfileCreationForm(UserCreationForm):
    username = forms.CharField(label="Username", max_length=40, min_length=5)
    email = forms.EmailField(label="Email address", max_length=40)
    first_name = forms.CharField(label='First name', max_length=40, required=False)
    last_name = forms.CharField(label='Last name', max_length=40, required=False)

    password1 = forms.CharField(label="Password", widget=forms.PasswordInput, min_length=5)
    password2 = forms.CharField(label="Password confirmation", widget=forms.PasswordInput)

    class Meta():
        model = UserProfile
        fields = (
            'username', 'email', 'type_of_user', 'first_name', 'last_name', 'password1', 'password2','IBAN',

        )

    def clean_password1(self):
        password = self.cleaned_data.get('password1')
        if len(password) < 8:
            raise ValidationError('Password has to be of size at least 8 characters')
        return super(UserProfileCreationForm, self).clean_password1()

    def clean_password2(self):
        password1 = self.cleaned_data.get("password1")
        password2 = self.cleaned_data.get("password2")
        if password1 and password2 and password1 != password2:
            raise forms.ValidationError("Password mismatch")
        return password2

    def save(self, commit=True):
        try:
            with transaction.atomic():
                user = User(username=self.cleaned_data['username'],
                            first_name=self.cleaned_data['first_name'],
                            last_name=self.cleaned_data['last_name'],
                            email=self.cleaned_data['email'])
                user.save()
                user.set_password(self.cleaned_data["password1"])

                user_profile = UserProfile(user=user,
                                           IBAN=self.cleaned_data['IBAN'],
                                           type_of_user=self.cleaned_data['type_of_user']
                                           )
                user_profile.save()
        except:
            raise #TODO: HANDLE THE EXCEPTION

        return user

【问题讨论】:

    标签: python django django-forms django-registration


    【解决方案1】:

    您可以安全地删除clean_password2 - Django 已经验证了密码是否匹配(我假设您正在为您的新项目使用当前版本)。至于clean_password1,我建议也将其删除并阅读password validation 上的文档(Django 1.9 中的新功能)。

    【讨论】:

      【解决方案2】:

      没有理由在 clean_field 方法中调用super;该字段在基类中不存在,因此 clean_field 也不存在。

      【讨论】:

        猜你喜欢
        • 2016-09-19
        • 1970-01-01
        • 1970-01-01
        • 2021-07-12
        • 2020-11-17
        • 1970-01-01
        • 2018-02-11
        • 2020-09-03
        • 1970-01-01
        相关资源
        最近更新 更多