Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

198
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda