TypeError: __init__() got an unexpected keyword argument 'attrs'

Question:

I know this question had been asked many times but I still cant figure it out yet.

from django.contrib.auth.forms import UserCreationForm
from django.forms import ModelForm
from django import forms
from . models import Profile
from django.contrib.auth.models import User

ACCOUNT_TYPE = [
    ('SPO', 'SPO'),
    ('Call Agent', 'Call Agent'),
    ('Accountant', 'Accountant'),
]

class CreateUserForm(UserCreationForm):
    name            = forms.CharField(max_length=255, required=False)
    account_type    = forms.ChoiceField(choices = ACCOUNT_TYPE)

    
    class Meta:
        model = User
        fields = ['username', 'email', 'password1', 'password2']

        widgets = {
            'name': forms.CharField(attrs={'class': 'form-control','required':'required'}),
            'account_type': forms.ChoiceField(attrs={'class': 'form-control','required':'required'})
        }

What am I doing wrong. I have made every possible changes but nothing had changed.
Thank you in advance.

Asked By: Muhammed Mahir

||

Answers:

The forms.CharField(...) is a Django Form Field, which is not a widget. Use forms.TextInput(...) instead

widgets = {
    'name': forms.TextInput(attrs={'class': 'form-control', 'required': 'required'}),
    'account_type': forms.TextInput(attrs={'class': 'form-control', 'required': 'required'})
}
Answered By: JPG

You mistakenly use the form field forms.CharField() for "widgets":

widgets = {
    'name': forms.CharField(attrs={...}),
    'account_type': forms.ChoiceField(attrs={...})
}

Instead, you need to use the widget forms.TextInput() for "widgets":

widgets = {
    'name': forms.TextInput(attrs={...}),
    'account_type': forms.TextInput(attrs={...})
}
Answered By: Kai – Kazuya Ito