2012-09-26 94 views
5
class MyForm(forms.Form): 
    CHOICES = (('1', 'one',), ('2', 'two',)) 
    one_or_two = forms.ChoiceField(widget=forms.RadioSelect, initial='1') 

def show(request): 
    form = MyForm() 
    # render form 

如何讓外地one_or_two只讀?如何使ChoiceField只讀Django的形式

+0

你試過看這個嗎? http://stackoverflow.com/questions/324477/in-a-django-form-how-to-make-a-field-readonly-or-disabled-so-that-it-cannot-b – Amit

+0

是的,它doesn不爲我工作。我正在擴展forms.Form,而不像ModelForm那樣在尖銳的問題中。 – Nullpoet

回答

8

您可以使用禁用的屬性。

one_or_two = forms.ChoiceField(widget=forms.RadioSelect(attrs={'disabled': 'disabled'}), initial='1') 
+0

它的工作原理..謝謝! – Nullpoet