9

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

4

2 回答 2

18

attrs不是字段的参数,而是小部件的参数。

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

请注意,某些浏览器不允许对文件输入进行样式设置。

于 2013-05-10T12:41:26.940 回答
2

尽管@Daniel Roseman 发布的解决方案也是 Django 文档中推荐的解决方案,但它仍然对我不起作用。对我有用的是以下内容:

class ImportExcelForm(Form):
    file  = forms.FileField()
    file.widget.attrs.update({'class': 'rounded_list'})
于 2020-04-23T20:41:22.430 回答