Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 太多值无法解包(应为2个)Django_Python_Django_Forms_Choicefield - Fatal编程技术网

Python 太多值无法解包(应为2个)Django

Python 太多值无法解包(应为2个)Django,python,django,forms,choicefield,Python,Django,Forms,Choicefield,我正在创建一个表单,该表单包含一个选择字段,我通过查看来确定所选值 根据Django文件,选择值必须为: 要用作此字段选项的2元组的iterable(例如,列表或元组),或返回此类iterable的callable。此参数接受与模型字段的choices参数相同的格式 加载视图时,我没有遇到任何问题。但是,在验证视图中的表单后,当我尝试获取选择字段的值时,我得到了一个错误太多值,无法解压缩(预期为2个) 我不知道是否在选项字段中错误地添加了选项值。虽然我想如果是这样,视图也不会加载。我做错了什么

我正在创建一个表单,该表单包含一个
选择字段
,我通过查看来确定所选值

根据Django文件,选择值必须为:

要用作此字段选项的2元组的iterable(例如,列表或元组),或返回此类iterable的callable。此参数接受与模型字段的choices参数相同的格式

加载视图时,我没有遇到任何问题。但是,在验证视图中的表单后,当我尝试获取
选择字段的值时,我得到了一个错误
太多值,无法解压缩(预期为2个)

我不知道是否在
选项字段
中错误地添加了选项值。虽然我想如果是这样,视图也不会加载。我做错了什么

forms.py

class FormAffiliateReport(forms.Form):

...

referrals = forms.ChoiceField(choices=(), label='Choice Referral', widget=forms.Select(attrs={'class': 'form-control',}))

def __init__(self, referrals, *args, **kwargs):
    super(FormAffiliateReport, self).__init__(*args, **kwargs)
    self.fields['referrals'].choices = referrals
views.py

def affiliate_report(request):
if request.session.has_key('affiliate_code'):
    affiliates = []
    affiliate_code = request.session['affiliate_code']
    affiliates = get_affiliates(affiliates, affiliate_code)
    affiliates.sort(key=lambda affiliate: affiliate.name.title())
    if request.method == 'POST':
        form = FormAffiliateReport(request.POST)
        if form.is_valid():
            referrals = form.data['referrals']
            return render(request, 'blog/affiliate_report.html', {"affiliate_code": affiliate_code, "form": form})
    else:
        choices = ()
        for affiliate in affiliates:
            choices = choices + ((str(affiliate.code), affiliate.name),)
        form = FormAffiliateReport(choices)

    return render(request, 'blog/affiliate_report.html', {"affiliate_code": affiliate_code, "form": form})
else:
    return redirect('home')
回溯

File "C:\Users\pc\Environments\company\lib\site-packages\django\core\handlers\exception.py", line 41, in inner
response = get_response(request)
File "C:\Users\pc\Environments\company\lib\site-packages\django\core\handlers\base.py", line 187, in _get_response
response = self.process_exception_by_middleware(e, request)
File "C:\Users\pc\Environments\company\lib\site-packages\django\core\handlers\base.py", line 185, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Users\pc\Projects\company\blog\views.py", line 224, in affiliate_report
return render(request, 'blog/affiliate_report.html', {"affiliate_code": affiliate_code, "form": form})
File "C:\Users\pc\Environments\company\lib\site-packages\django\shortcuts.py", line 30, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "C:\Users\pc\Environments\company\lib\site-packages\django\template\loader.py", line 68, in render_to_string
return template.render(context, request)
File "C:\Users\pc\Environments\company\lib\site-packages\django\template\backends\django.py", line 66, in render
return self.template.render(context)
File "C:\Users\pc\Environments\company\lib\site-packages\django\template\base.py", line 207, in render
return self._render(context)
File "C:\Users\pc\Environments\company\lib\site-packages\django\template\base.py", line 199, in _render
return self.nodelist.render(context)
File "C:\Users\pc\Environments\company\lib\site-packages\django\template\base.py", line 990, in render
bit = node.render_annotated(context)
File "C:\Users\pc\Environments\company\lib\site-packages\django\template\base.py", line 957, in render_annotated
return self.render(context)
File "C:\Users\pc\Environments\company\lib\site-packages\django\template\defaulttags.py", line 216, in render
nodelist.append(node.render_annotated(context))
File "C:\Users\pc\Environments\company\lib\site-packages\django\template\base.py", line 957, in render_annotated
return self.render(context)
File "C:\Users\pc\Environments\company\lib\site-packages\django\template\base.py", line 1046, in render
return render_value_in_context(output, context)
File "C:\Users\pc\Environments\company\lib\site-packages\django\template\base.py", line 1024, in render_value_in_context
value = force_text(value)
File "C:\Users\pc\Environments\company\lib\site-packages\django\utils\encoding.py", line 76, in force_text
s = six.text_type(s)
File "C:\Users\pc\Environments\company\lib\site-packages\django\utils\html.py", line 385, in <lambda>
klass.__str__ = lambda self: mark_safe(klass_str(self))
File "C:\Users\pc\Environments\company\lib\site-packages\django\forms\boundfield.py", line 41, in __str__
return self.as_widget()
File "C:\Users\pc\Environments\company\lib\site-packages\django\forms\boundfield.py", line 94, in as_widget
attrs = self.build_widget_attrs(attrs, widget)
File "C:\Users\pc\Environments\company\lib\site-packages\django\forms\boundfield.py", line 250, in build_widget_attrs
if widget.use_required_attribute(self.initial) and self.field.required and self.form.use_required_attribute:
File "C:\Users\pc\Environments\company\lib\site-packages\django\forms\widgets.py", line 690, in use_required_attribute
return use_required_attribute and first_choice is not None and self._choice_has_empty_value(first_choice)
File "C:\Users\pc\Environments\company\lib\site-packages\django\forms\widgets.py", line 673, in _choice_has_empty_value
value, _ = choice
ValueError: too many values to unpack (expected 2)
文件“C:\Users\pc\Environments\company\lib\site packages\django\core\handlers\exception.py”,第41行,在内部
响应=获取响应(请求)
文件“C:\Users\pc\Environments\company\lib\site packages\django\core\handlers\base.py”,第187行,在\u get\u响应中
response=self.process\u异常\u由\u中间件(e,请求)
文件“C:\Users\pc\Environments\company\lib\site packages\django\core\handlers\base.py”,第185行,在get\U响应中
响应=包装的回调(请求,*回调参数,**回调参数)
“C:\Users\pc\Projects\company\blog\views.py”文件,第224行,在附属公司报告中
返回呈现(请求'blog/affiliate_report.html',{“affiliate_code”:affiliate_code,“form”:form})
文件“C:\Users\pc\Environments\company\lib\site packages\django\shortcuts.py”,第30行,在render中
content=loader.render_to_string(模板名称、上下文、请求、using=using)
文件“C:\Users\pc\Environments\company\lib\site packages\django\template\loader.py”,第68行,呈现为字符串
返回template.render(上下文、请求)
文件“C:\Users\pc\Environments\company\lib\site packages\django\template\backends\django.py”,第66行,在render中
返回self.template.render(上下文)
文件“C:\Users\pc\Environments\company\lib\site packages\django\template\base.py”,第207行,在render中
返回self.\u呈现(上下文)
文件“C:\Users\pc\Environments\company\lib\site packages\django\template\base.py”,第199行,在\u render中
返回self.nodelist.render(上下文)
文件“C:\Users\pc\Environments\company\lib\site packages\django\template\base.py”,第990行,在render中
位=节点。带注释的渲染(上下文)
文件“C:\Users\pc\Environments\company\lib\site packages\django\template\base.py”,第957行,在带注释的render\u中
返回self.render(上下文)
文件“C:\Users\pc\Environments\company\lib\site packages\django\template\defaulttags.py”,第216行,在render中
nodelist.append(node.render_注释(上下文))
文件“C:\Users\pc\Environments\company\lib\site packages\django\template\base.py”,第957行,在带注释的render\u中
返回self.render(上下文)
文件“C:\Users\pc\Environments\company\lib\site packages\django\template\base.py”,第1046行,在render中
在上下文(输出,上下文)中返回渲染值
文件“C:\Users\pc\Environments\company\lib\site packages\django\template\base.py”,第1024行,位于\u上下文中的render\u value\u中
值=强制文本(值)
文件“C:\Users\pc\Environments\company\lib\site packages\django\utils\encoding.py”,第76行,强制文本
s=六个。文本类型
文件“C:\Users\pc\Environments\company\lib\site packages\django\utils\html.py”,第385行,在
克拉斯·斯特尔:马克·萨弗(克拉斯·斯特尔)
文件“C:\Users\pc\Environments\company\lib\site packages\django\forms\boundfield.py”,第41行,在__
返回self.as_小部件()
文件“C:\Users\pc\Environments\company\lib\site packages\django\forms\boundfield.py”,第94行,在as\U小部件中
attrs=self.build\u widget\u attrs(attrs,widget)
文件“C:\Users\pc\Environments\company\lib\site packages\django\forms\boundfield.py”,第250行,在build\u widget\u attrs中
如果widget.use_required_属性(self.initial)和self.field.required和self.form.use_required_属性:
文件“C:\Users\pc\Environments\company\lib\site packages\django\forms\widgets.py”,第690行,in use\u required\u属性
返回use_required_属性,并且first_choice不是None和self。_choice_有_empty_值(first_choice)
文件“C:\Users\pc\Environments\company\lib\site packages\django\forms\widgets.py”,第673行,在选项中有空值
值,即选择
ValueError:要解压缩的值太多(应为2个)

要将我已经在评论中的内容正式化:

OP中的问题是,当表单被实例化为
Form=formAffiliaterReport(request.POST)
时,
表单的构造函数的
references
参数承担了整个
POST
数据。所需要的是使用关键字参数来表示动态变化的选择

因此,在视图中,执行以下操作:

choices = ... # some computation, specific to the OP's needs
form = FormAffiliateReport(request.POST, choices=choices)
表格
类中:

def __init__(self, *args, **kwargs):
    choices = kwargs.pop("choices")
    super(FormAffiliateReport, self).__init__(*args, **kwargs)
    self.fields['referrals'].choices = choices

您能显示完整的回溯吗?这一行看起来错误:
self.fields['referrals'].choices=referrals
。您正试图将所有
POST
数据传递到选择字段。@RobinZigmond那么我应该以什么方式修改该行?我被这个回复所引导,将选项发送到ChoiceField@RobinZigmond,这对我来说很有效。非常感谢您的帮助和耐心。谢谢,没问题:)我已经正式将此作为答案,如果您接受,我将不胜感激。