我设法为TextInput和EmailField设置属性,但不适用于PasswordInput ...
我的注册表格:
class RegisterForm(UserCreationForm):
class Meta:
model = User
fields = ("username", "email", "password1", "password2")
widgets = {
'username': forms.TextInput(attrs = {'class': 'nice_input', 'placeholder': 'Name'}),
'email': forms.EmailInput(attrs = {'class': 'nice_input', 'placeholder': 'Email'}),
'password1': forms.PasswordInput(attrs = {'class': 'nice_input', 'placeholder': 'Password'}),
'password2': forms.PasswordInput(attrs = {'class': 'nice_input', 'placeholder': 'Password confirmation'}),
}
在admin.py中输入我的UserCreationForm:
class UserCreationForm(forms.ModelForm):
password1 = forms.CharField()
password2 = forms.CharField()
class Meta:
model = User
fields = ('email',)
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("Passwords don't match")
return password2
def save(self, commit=True):
user = super().save(commit=False)
user.set_password(self.cleaned_data["password1"])
if commit:
user.save()
return user
对于用户名和电子邮件字段,一切正常,但对于其他字段则不行...
我应该尝试什么?