I'm trying to create a custom fields in Django
I'm not sure this is the right approach, but I keep getting an error I can't understandenter code here
AttributeError: 'CharField' object has no attribute 'attrs'
Here is my code. Can anyone explain what's going on?
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"})),
}
You don't use the widgets parameter correctly. The values you set for the confirm password and password are fields when they should be widgets. You should be using forms.PasswordInput directly.
If you want to customize the classes which are used for the form's fields instead of having them derived from the ones specified in the model you need to provide the field_classes attribute.