[英]'super' object has no attribute 'clean_password1'
I'm trying to reuse a code for registration form from my older project. 我试图重用旧项目中的注册表格代码。 The problem is that Django says that UserCreationForm
hasn't attribute clean_password1
. 问题在于Django说UserCreationForm
没有属性clean_password1
。
Do you know where is the problem? 您知道问题出在哪里吗? I see that there is not such attribute in UserCreationForm
but it worked before. 我看到UserCreationForm
没有这样的属性,但是以前可以使用。
What should I do to make it work? 我应该怎么做才能使其正常工作?
EDIT: It's because it calls super(...).clean_password1 which isn't in super class. 编辑:这是因为它调用不在超类中的super(...)。clean_password1。 So I've tried to put there super(...).cleaned_data.get("password1"), but it raises error that cleaned_data isn't there (in superclass). 因此,我尝试将super(...)。cleaned_data.get(“ password1”)放到那里,但是这会引发错误,即cleaned_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
You can safely remove clean_password2
- Django already validates if the passwords match (I assume you're using the current version for your new project). 您可以安全地删除clean_password2
-Django已验证密码是否匹配(我假设您正在为新项目使用当前版本)。 As for clean_password1
I'd recommend to remove it too and read documentation on password validation (new in Django 1.9). 至于clean_password1
我建议也将其删除,并阅读有关密码验证的文档(Django 1.9中的新增功能)。
There's no reason to call super
inside a clean_field method; 没有理由在clean_field方法中调用super
。 the field doesn't exist on the base class, so neither does the clean_field. 该字段在基类上不存在,因此clean_field也不存在。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.