Django - email form to choose from

I created a simple django form with a list of options (in the radio buttons):

class MyForm(forms.Form): choices=forms.ChoiceField( widget=forms.RadioSelect(), choices=[(k,k) for k in ['one','two','three']],label="choose one") 

I would like the form to submit automatically when the user selects one of the options. In direct HTML, I would do it like

  <select name='myselect' onChange="FORM_NAME.submit();"> .... </select> 

But I do not know how to integrate this into a form class without writing a template. In particular, I need to know FORM_NAME , so I can call FORM_NAME.submit() in the above snippet. Can this be done without using a template?

+7
source share
1 answer

I think you do not need to know the name of the form. This should also work:

 <select name='myselect' onChange="this.form.submit();"> 

A quick fix to integrate this into your form should include adding an attribute to your widget .

 widget=forms.RadioSelect(attrs={'onchange': 'this.form.submit();'}) 

Now it can be argued that this is no better separated from your definition of form (separation of definition, style and behavior), but that should do it.

+14
source

All Articles