Django 提交更改表单

Han*_*ong 4 python forms django submit django-forms

您好,我正在尝试在选择一个选项时提交表单 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();'})
Run Code Online (Sandbox Code Playgroud)

但是现在当我尝试加载表单时,我得到了一个无效的语法。我很确定这attrs={'onchange': 'actionform.submit();'})是问题所在,但不知道该怎么做。

ale*_*cxe 7

您需要widget在字段上设置一个参数并传递attrs参数:

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

此外,该choices列表应包含包含两个内容的项目:

choices = [(0, 'create new folder'), (1, 'delete'), (2, 'read'), (3, 'unread')]
Run Code Online (Sandbox Code Playgroud)