Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

__init__() got an unexpected keyword argument 'attrs'

forms.py:

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

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

What did I do wrong?

Thanks.

like image 432
user2086641 Avatar asked May 10 '13 12:05

user2086641


2 Answers

Even though the solution posted by @Daniel Roseman is also the one recommended in Django docs, it still didn't work for me. What worked for me is the following:

class ImportExcelForm(Form):
    file  = forms.FileField()
    file.widget.attrs.update({'class': 'rounded_list'})
like image 134
Milosz Avatar answered Nov 08 '22 02:11

Milosz


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.

like image 19
Daniel Roseman Avatar answered Nov 08 '22 01:11

Daniel Roseman