我创建了一个注册表单和两个自定义用户类型;学生和家长。注册适用于两个用户。问题是当我去django admin并查看我的用户时,学生的密码是在散列算法中,而对于父用户,密码字段说"无效的密码格式或未知的散列算法"。我不确定问题源自何处,但假设它来自我的父表单继承寄存器表单。任何帮助将不胜感激,谢谢!
forms.py
tasks.vs.json
views.py
File->Open->Open Folder
答案 0 :(得分:1)
set_password
是一种方法,因此我建议您执行以下操作:
替换:
user.set_password = self.cleaned_data["password"]
与
user.set_password(self.cleaned_data["password"])
答案 1 :(得分:0)
我不能肯定地说,但是你的继承形式正在复制他们父母的方法。
例如,RegisterForm.save
和StudentSignUpForm
中有ParentSignUpForm
重复。
您还可以在子项中覆盖Meta
不同的数据。
在所有这些重复和Django魔法之间,我会简化为类似下面的东西并从那里开始。
注意:我没有测试下面的内容,只是删除了一些重复内容。希望现在调试起来更容易。
class RegisterForm(forms.ModelForm):
email = forms.EmailField(widget=forms.EmailInput(attrs={'class' : 'form-control', 'placeholder' : 'Email', 'id' : 'email', 'required' : 'true'}))
first_name = forms.CharField(widget=forms.TextInput(attrs={'class' : 'form-control', 'placeholder' : 'First name', 'id' : 'first-name', 'required' : 'true'}))
last_name = forms.CharField(widget=forms.TextInput(attrs={'class' : 'form-control', 'placeholder' : 'Last name', 'id' : 'last-name', 'required' : 'true'}))
password = forms.CharField(widget=forms.PasswordInput(attrs={'class' : 'form-control', 'placeholder' : 'Password', 'id' : 'password', 'required' : 'true'}))
password2 = forms.CharField(widget=forms.PasswordInput(attrs={'class' : 'form-control', 'placeholder' : 'Confirm password', 'id' : 'password2', 'required' : 'true'}))
class Meta:
model = User
# what fields from that model you want
fields = ('first_name', 'last_name', 'email', 'password', 'password2')
def clean_email(self):
email = self.cleaned_data.get('email')
qs = User.objects.filter(email=email)
if qs.exists():
raise forms.ValidationError("Email is taken")
return email
def clean_password2(self):
# Check that the two password entries match
password = self.cleaned_data.get("password")
password2 = self.cleaned_data.get("password2")
if password and password2 and password != password2:
raise forms.ValidationError("Passwords don't match")
return password2
def save(self, commit=True):
# Save the provided password in hashed format
user = super(RegisterForm, self).save(commit=False)
user.first_name = self.cleaned_data["first_name"]
user.last_name = self.cleaned_data["last_name"]
user.set_password = self.cleaned_data["password"]
#user.active = False send confirmation email
if commit:
user.save()
return user
class StudentSignUpForm(RegisterForm):
def save(self, commit=True):
user = super(StudentSignUpForm, self).save(commit=False)
user.student = True
if commit:
user.save()
return user
class ParentSignUpForm(RegisterForm):
first_name = forms.CharField(widget=forms.TextInput(attrs={'class' : 'form-control', 'placeholder' : 'Parent first name', 'id' : 'parent-first', 'required' : 'true'}))
last_name = forms.CharField(widget=forms.TextInput(attrs={'class' : 'form-control', 'placeholder' : 'Parent last name', 'id' : 'parent-last', 'required' : 'true'}))
child_first_name = forms.CharField(widget=forms.TextInput(attrs={'class' : 'form-control', 'placeholder' : "Child's first name", 'id' : 'child-first', 'required' : 'true'}))
def save(self, commit=True):
user = super(ParentSignUpForm, self).save(commit=False)
user.child_first_name = self.cleaned_data["child_first_name"]
user.parent = True
if commit:
user.save()
return user