当前位置: 动力学知识库 > 问答 > 编程问答 >

Django: how to submit form whes user click radiobutton

问题描述:

How to add:

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

in my radiobutton form? I would like to post my form when user click to the radiobutton.

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())

I would like to have this result in template:

<input type="radio" id="new" name="new" value="new" onclick="this.form.submit();">

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

while it's not the best idea to place template logic in your .py files.

网友答案:

How do you generate your form in a template? If you use {{ form.as_p }} than consider rendering your custom form like described in: Django's Custom Forms

分享给朋友:
您可能感兴趣的文章:
随机阅读: