[英]'super' object has no attribute 'clean_password1'
我試圖重用舊項目中的注冊表格代碼。 問題在於Django說UserCreationForm
沒有屬性clean_password1
。
您知道問題出在哪里嗎? 我看到UserCreationForm
沒有這樣的屬性,但是以前可以使用。
我應該怎么做才能使其正常工作?
編輯:這是因為它調用不在超類中的super(...)。clean_password1。 因此,我嘗試將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
您可以安全地刪除clean_password2
-Django已驗證密碼是否匹配(我假設您正在為新項目使用當前版本)。 至於clean_password1
我建議也將其刪除,並閱讀有關密碼驗證的文檔(Django 1.9中的新增功能)。
沒有理由在clean_field方法中調用super
。 該字段在基類上不存在,因此clean_field也不存在。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.