Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

django submit form on change

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

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.

like image 991
Hans de Jong Avatar asked Mar 19 '23 11:03

Hans de Jong


1 Answers

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')]
like image 91
alecxe Avatar answered Mar 23 '23 06:03

alecxe