如何调整Django的文件选择器窗体按钮? [英] How can you style Django's file picker form button?
本文介绍了如何调整Django的文件选择器窗体按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图对我的Django 文件上传按钮进行风格,但由于它是通过表单处理的,并且没有明确地写在模板中的HTML中,因此我无法直接使用HTML和CSS来设置样式,用于其他按钮类型输入。
I am attempting to style my Django file upload button, but since it's handled through the form and not explicitly written in the HTML within the template I cannot style it directly with HTML and CSS like I can for other buttons of type input.
我试图在我的 forms.py
中添加我的CSS类,
I've attempted to add my CSS class within my forms.py
, but it is placing the vanilla default Django file upload button on top of my CSS styled button.
我的代码如下:
class FileUploadForm(forms.Form):
docFile = forms.FileField(
label = "Select a CSV file",
)
class Meta:
model = FileUpload
fields = ('docFile')
def __init__(self, *args, **kwargs):
super(FileUploadForm, self).__init__(*args, **kwargs)
self.fields['docFile'].widget.attrs.update({'class': 'my_class'})
我还尝试在我的 Meta中定义
/ code> class like like:
I also tried defining a widget
within my Meta
class like so:
class Meta:
model = FileUpload
widgets = {
'docFile': forms.FileInput(attrs={'class': 'my_class'}),
}
但这与我的第一次尝试有相同的效果。
but that had the same effect as my first attempt.
有不同的方法来完成这个或有一些逻辑错误