【发布时间】:2017-10-01 10:16:50
【问题描述】:
我正在尝试在 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"})),
}
【问题讨论】: