2012-01-13 16 views
0

如何添加:Django的:如何提交表單whes用戶點擊單選按鈕

onclick="this.form.submit();" 

在我的單選按鈕形式?我想在用戶點擊單選按鈕時發佈我的表單。

class MyForm(forms.Form): 

def __init__(self, *args, **kwargs): 
    self.news = kwargs.pop('news') 
    super(MyForm, self).__init__(*args, **kwargs) 
    choices = ([ ("%s" % a.id, "%s" % a.text) for a in self.news]) 
    self.fields['new'] = forms.ChoiceField(choices = choices, widget=forms.RadioSelect()) 

我想有這樣的結果模板:

​​

回答

3
self.fields['new'] = forms.ChoiceField(choices = choices, widget=forms.RadioSelect(attrs={'onclick': 'this.form.submit();'})) 

雖然它並不放置模板邏輯在你的.py文件的最好的主意。

相關問題