2010-12-07 248 views
0

你知道如何改變的複選框代碼這個名單+清單+列表:Django表單複選框,單選按鈕

<p>How did you reach this site? <select name="howreach"> 
<option value="0" selected="selected">Choose one...</option> 
<option value="1">Typed the URL directly</option> 
<option value="2">Site is bookmarked</option> 
<option value="3">A search engine</option> 
<option value="4">A link from another site</option> 
<option value="5">From a book</option> 
<option value="6">Other</option> 
</select></p> 

到Django的形式?

而如何在Django的形式?:

Poor <input type="radio" name="rating" value="1" /> 1 
<input type="radio" name="rating" value="2" /> 2 
<input type="radio" name="rating" value="3" /> 3 
<input type="radio" name="rating" value="4" /> 4 
<input type="radio" name="rating" value="5" /> 5 Excellent</p> 

回答

2

在Python代碼更改爲單選按鈕的列表:

class SiteReach(forms.Form): 
    howreach = forms.ChoiceField(label = "How did you reach this site?", 
           choices = HOWREACH_CHOICES, 
           widget = forms.widgets.CheckboxInput()) 

你必須初始化你自己HOWREACH_CHOICES;它是一個元組列表,(選項值,選項字符串)。

您呈現以同樣的方式單選按鈕:

class Rating(forms.Form): 
    rating = forms.ChoiceField(choices = range(1,6), 
           widget = forms.widgets.RadioSelect()) 

閱讀窗口小部件的文件;那裏有一個完整的效用世界。

+0

好的,非常感謝你讓我這樣的複選框,但我不能確定它是正確的。非常感謝! – Iwona 2010-12-07 22:31:28