好吧,这是第三天寻找该错误的解决方案,所以我只想在这里问。
我知道还有其他与我类似的问题,但显然其他所有问题都有另一个原因。 这是我的设置: Django版本:2.1.5 Python版本:3.7.1
我的错误是这样的:
ValueError at /testsel
too many values to unpack (expected 2)
这是我的回溯:
Traceback (most recent call last):
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/core/handlers/exception.py", line 34, in inner
response = get_response(request)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/core/handlers/base.py", line 126, in _get_response
response = self.process_exception_by_middleware(e, request)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/core/handlers/base.py", line 124, in _get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "/home/ricardo/Projects/Organizacional/organizacional/home/views.py", line 36, in testsel
return render(request,'home/testsel.html',{ 'form':form })
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/shortcuts.py", line 36, in render
content = loader.render_to_string(template_name, context, request, using=using)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/template/loader.py", line 62, in render_to_string
return template.render(context, request)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/template/backends/django.py", line 61, in render
return self.template.render(context)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 171, in render
return self._render(context)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/template/loader_tags.py", line 150, in render
return compiled_parent._render(context)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 163, in _render
return self.nodelist.render(context)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/template/loader_tags.py", line 62, in render
result = block.nodelist.render(context)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 937, in render
bit = node.render_annotated(context)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 904, in render_annotated
return self.render(context)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 993, in render
return render_value_in_context(output, context)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/template/base.py", line 972, in render_value_in_context
value = str(value)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/utils/html.py", line 397, in <lambda>
klass.__str__ = lambda self: mark_safe(klass_str(self))
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/forms/forms.py", line 142, in __str__
return self.as_table()
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/forms/forms.py", line 284, in as_table
errors_on_separate_row=False,
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/forms/forms.py", line 202, in _html_output
top_errors = self.non_field_errors() # Errors that should be displayed above all fields.
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/forms/forms.py", line 313, in non_field_errors
return self.errors.get(NON_FIELD_ERRORS, self.error_class(error_class='nonfield'))
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/forms/forms.py", line 180, in errors
self.full_clean()
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/forms/forms.py", line 381, in full_clean
self._clean_fields()
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/forms/forms.py", line 393, in _clean_fields
value = field.widget.value_from_datadict(self.data, self.files, self.add_prefix(name))
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/forms/widgets.py", line 648, in value_from_datadict
return getter(name)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/db/models/query.py", line 390, in get
clone = self.filter(*args, **kwargs)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/db/models/query.py", line 844, in filter
return self._filter_or_exclude(False, *args, **kwargs)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/db/models/query.py", line 862, in _filter_or_exclude
clone.query.add_q(Q(*args, **kwargs))
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1263, in add_q
clause, _ = self._add_q(q_object, self.used_aliases)
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1287, in _add_q
split_subq=split_subq,
File "/home/ricardo/anaconda3/lib/python3.7/site-packages/django/db/models/sql/query.py", line 1161, in build_filter
arg, value = filter_expr
ValueError: too many values to unpack (expected 2)
这是在views.py中调用的函数:
def testsel(request):
if request.method=="POST":
pass #just testing for now
else:
form=SelectTest(Tests.objects.all())
return render(request,'home/testsel.html',{ 'form':form })
这是在forms.py中定义的表单:
class SelectTest(forms.Form):
def __init__(self, *args, **kwargs):
super(SelectTest,self).__init__(*args,**kwargs)
CHOICES=args[0].values('sigla')
CHOICES=[(s['sigla'],s['sigla'].lower()) for s in CHOICES]
self.fields['select']=forms.ChoiceField(widget=forms.Select(),choices=CHOICES)
这是我的模板
{% block content %}
<form action="" method="post">{% csrf_token %}
{{ form }}
<button type="submit">Enviar</button>
</form>
{% endblock content %}
这是在views.py中调用的测试(尽管我认为这并不重要):
class Tests(models.Model):
name = models.CharField(max_length=100)
sigla = models.CharField(max_length=10)
instruction = models.TextField()
about = models.TextField()
nquestions = models.PositiveIntegerField()
table = models.CharField(max_length=10)
现在,如果我将模板更改为此:
{% for field in form %}
{{ field.select }}
{% endfor %}
它呈现没有错误,但是表单没有呈现,或出现在“查看源代码”中。如果我在循环内仅使用field
,则会返回到“要解压缩的值太多”。
我知道我需要在选择时传递一个元组列表,这就是完成的工作。我已经对其进行了三重检查。
感谢任何帮助。
答案 0 :(得分:2)
表单的第一个参数是与其绑定的数据。您要在此处传递查询集,然后立即将其传递给超类,这会导致错误。
如果要向表单传递额外的数据,则应始终使用关键字参数,并在调用super之前将其从kwargs字典中删除。
form=SelectTest(sigla=Tests.objects.all())
...
class SelectTest(forms.Form):
def __init__(self, *args, **kwargs):
sigla = kwargs.pop('sigla', None)
super(SelectTest,self).__init__(*args,**kwargs)
请注意,您可能应该使用ModelChoiceField-直接接受查询集-而不是简单的ChoiceField。