Search code examples
pythonpython-3.xdjangodjango-formsdjango-widget

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


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.


Solution

  • 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'})
    }