How to use attrs in forms.CharField (widget = forms.PasswordInput ()

I'm trying to create custom fields in Django I'm not sure if this is the right approach, but I keep getting an error that I don't understand. enter code here

AttributeError: 'CharField' object has no attribute 'attrs'

      

Here is my code. Can anyone explain what is 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"})),

    }

      

+3


source to share


1 answer


You are not using the parameter widgets

correctly. The values ​​set for confirm password

and password

are fields when they should be widgets. You have to use forms.PasswordInput

directly.



If you want to customize the classes that are used for the form fields, instead of getting them from those specified in the model, you need to specify the attribute field_classes

.

0


source







All Articles