Hans de Jong Hans de Jong - 5 months ago 42x
Python Question

django submit form on change

Hello I am trying to submit a form when selection an option on a


class ActionForm(forms.Form):
""" Holds the options for mailbox management """
choices = ['create new folder', 'delete', 'read', 'unread']
action = forms.ChoiceField(choices=choices, attrs={'onchange': 'actionform.submit();'})

but now I get an invallid syntax when I try to load the form. I am pretty sure the
attrs={'onchange': 'actionform.submit();'})
is the problem, but not sure how else to do it.


You need to set a widget argument on the field and pass attrs argument:

action = forms.ChoiceField(choices=choices, 
                           widget=forms.Select(attrs={'onchange': 'actionform.submit();'}))

Also, the choices list should contain items containing two things inside:

choices = [(0, 'create new folder'), (1, 'delete'), (2, 'read'), (3, 'unread')]