이전 프로젝트의 등록 양식 코드를 다시 사용하려고합니다. 문제는 Django가 UserCreationForm
은 clean_password1
속성이 없다고 말합니다.'super'객체에 'clean_password1'속성이 없습니다.
어디에서 문제가 발생 했습니까? 나는 UserCreationForm
에 그러한 속성이 없다는 것을 알았지 만 그것은 이전에 작동했습니다.
제대로 작동하려면 어떻게해야합니까?
편집 : 수퍼 클래스에없는 super (...). clean_password1을 호출하기 때문입니다. 그래서 거기에 슈퍼 (...). 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