Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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"})),

    }
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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.

about 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!