2013-10-08 10 views
0

我正在使用django小部件調整來顯示和調整我的字段在Django模板中,每一件事情都很好,但對於選擇框,當我呈現它時,它的初始顯示值是-----,因爲我想顯示我的自定義值的-----,像--Select a Country--
我的語法如下:Django模板 - 如何插入並在選擇框中放置一個值,默認情況下?

{{ form.countries }} // countries is a choice field, therefore it is displayed as a select box 

如果需要任何改變我不喜歡這樣寫道:

{{ form.countries|attr:"class:span6" }} //just for an example 

請幫我做這個, 我試過這個:

{{ form.countries|attr:"option:--Select a Country--" }} 

請幫幫我!
編輯將不勝感激!
謝謝。

回答

1

這應該在您使用initial屬性初始化表單的視圖中完成。

因此,像:

countries = forms.ChoiceField(
       choices=YourCountriesGoesHere, 
       initial='--- Select a country ---') 

您可以閱讀documentation here

相關問題