I've been trying to customize the label of the file upload button in my Django app. In Firefox, the button appears with the label 'Browse', and the hint text 'No files selected'. I need the label to be 'Upload', and want to customize the hint text too. Here's what does NOT work (from forms.py):
class PicsUploadForm(forms.ModelForm):
image = forms.ImageField(label='Upload')
image.widget.attrs["value"] ='Upload'
class Meta:
model = Pic
exclude = ("sender","sending_time", "expiry_interval")
fields = ("image",)
def __init__(self, *args, **kwargs):
super(PicsUploadForm, self).__init__(*args, **kwargs)
self.fields['image'].widget.attrs.update({'value':'Upload'})
It produces the following tag in html:
<input id="id_image" type="file" value="Upload" name="image"></input>
But the upload button still appears with 'Browse' label, instead of 'Upload'. I also tried the following (which doesn't work):
def __init__(self, *args, **kwargs):
super(PicsUploadForm, self).__init__(*args, **kwargs)
self.fields['image'].label='Upload'#["value"]='Upload'
However, I'm successfully able to change the button's label to 'Upload'
if I change the type from 'file'
to 'image'
. According to w3schools, this type defines an image as the submit button.
I was wondering if I could somehow use type="image"
to customize my file upload button's label (or make the functionality from scratch).
Does anyone have a working example for this, or will this never work?