__init__() got an unexpected keyword argument 'attrs'

Issue

forms.py

class ImportExcelForm(Form):
    file  = forms.FileField(attrs={'class':'rounded_list',})

I am trying to add css class to my filefield in forms.I am getting this error "__init__() got an unexpected keyword argument 'attrs'"

What i did wrong.

Thanks

Solution

attrs is not an argument to the field, it’s an argument to the widget.

file = forms.FileField(widget=forms.FileInput(attrs={'class': 'rounded_list'}))

Note that some browsers don’t allow styling of the file input.

Answered By – Daniel Roseman

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published