Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

200
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda