使用ChoiceField时的ValueError

时间:2015-09-25 21:04:56

标签: django django-forms

我想创建一个Django表单,允许用户从下拉菜单中选择选项,但我收到一个值错误:

  

ValueError:解包需要多于1个值

形式:

class DropdownForm(forms.Form):
  def __init__(self,*args,**kwargs):
    choices = kwargs.pop('choices')
    label = kwargs.pop('label')

    super(DropdownForm,self).__init__(*args,**kwargs)
    self.fields['selected'].label = mark_safe(label)
    self.fields['selected'].choices = choices

  selected = forms.ChoiceField(widget=forms.Select(attrs={'class':"form-control text-center"}))

View.py:

form_rate = DropdownForm(choices=[("HIGH","HIGH")],label="RATE",prefix="Rate")
form_pass_setup = DropdownForm(choices=[("AUTO","AUTO"),("MANUAL","MANUAL")],label="Pass Setup",prefix="pass_setup")
form_dict.update({'form_rate':form_rate,'form_pass_setup':form_pass_setup})

return render(request,'Nominal.html',form_dict)

模板:

<form action="/InterfaceApp/Nominal_Request/" method="post" class="form">
  {% csrf_token %}
  <div class="panel-body text-center">
    <div class="row pad_forms">
      <div class="col-xs-3">
        {% bootstrap_form form_rate %}
      </div>
      <div class="col-xs-3">
        {% bootstrap_form form_pass_setup %}
      </div>
    </div>
    <br><br>
    <button type="submit" class="btn btn-primary center-block" value="Submit" name="Single">
      {% bootstrap_icon "fire" %} Generate a Single Requests
    </button>
  </div>
</form>

有谁可以告诉我为什么我会收到此值错误?

1 个答案:

答案 0 :(得分:0)

由于您未为choices设置所需的参数ChoiceField,可能会出现此问题。

以下是文档的链接:https://docs.djangoproject.com/en/1.8/ref/forms/fields/#choicefield