我正在尝试在Django中创建自定义字段
我不确定这是正确的方法,但我一直收到一个我无法理解的错误enter code here
AttributeError: 'CharField' object has no attribute 'attrs'
这是我的代码。谁能解释一下发生了什么?
class UserForm(forms.ModelForm):
password = forms.CharField(widget=forms.PasswordInput())
confirm_password = forms.CharField(widget=forms.PasswordInput())
class Meta:
model = User
fields = ('username', 'email', 'password', 'confirm_password')
widgets = {
'username': forms.TextInput(attrs={'class': "form-control input-lg",
'placeholder': "نام کاربری *",
'name': 'display_name',
'required': "required",
'tabindex': "3",
'data-error': "password is required"}),
'email': forms.EmailInput(attrs={'class': "form-control input-lg",
'placeholder': 'ایمیل *',
'name': 'email',
'required': "required",
'tabindex': "4",
'data-error': "email is required"}),
'confirm_password': forms.CharField(widget=forms.PasswordInput(attrs={'class': "form-control input-lg",
'placeholder': "تکرار رمز *",
'name': 'confirm_password',
'required': "required",
'tabindex': "6",
'data-error': "confirm password is required"})),
'password': forms.CharField(widget=forms.PasswordInput(attrs={"class": "form-control input-lg",
'placeholder': " رمز *",
'name': 'password',
'required': "required",
'tabindex': "5",
'data-error': " password is required"})),
}
答案 0 :(得分:0)
您没有正确使用widgets
参数。您为confirm password
和password
设置的值是应该是小部件的字段。您应该直接使用forms.PasswordInput
。
如果要自定义用于表单字段的类,而不是从模型中指定的类派生它们,则需要提供field_classes
属性。