2014-09-03 64 views
1

說我有這樣的模式:Django的 - 改變什麼「上傳文件」按鈕,在模板

class uploadImage(models.Model): 
    postImage = models.ImageField(upload_to=get_postImage_path, blank=True, null=True) 

和我創建了一個形式爲這種模式。我將該表單作爲「imageForm」傳遞給我的模板。這是我的模板:

<form enctype="multipart/form-data" id='imageForm' method="post" action="/uploadImage/">{% csrf_token %} 
{{ imageForm.postImage }} 
<input type='submit' value='upload' /> 
</form> 

現在,當我去這個模板頁面從我的瀏覽器,有一個按鈕,說:「選擇文件」和按鈕旁邊,它說:「沒有選擇任何文件」。在用戶點擊'選擇文件'並選擇一個文件後,'沒有選擇文件'的文本變爲文件名。

一旦用戶選擇圖像,我該如何讓按鈕說'上傳圖像'而不是'選擇文件',並使'沒有選擇文件'說'附加圖像'(而不是文件名)?

+0

[在輸入類型中更改默認文本=「文件」?](http://stackoverflow.com/questions/5138719/change-default-text-in-input-type-file) – Brandon 2014-09-03 19:00:47

回答

1

這與Django無關;文件上傳按鈕和文本由瀏覽器定義。有解決方案,其中大部分涉及使用觸發上傳按鈕的另一個元素。

Here's a five-minute YouTube tutorial解決了Django應用程序的問題。

This jQuery plugin也將解決您的問題。

And this solution使用CSS。


此外,以下是一些鏈接到其他的解決方案和討論:

How to change the button text of <input type="file" />?

Labeling file upload button

HTML input file - how to translate "Choose File" and "No file Chosen"?

Change default text in input type="file"?