ValueError:没有足够的值来解包(预期2,得1)

以下是我的代码

views.py

from django.shortcuts import render
from .forms import MedicineForm
from .models import Medicine

def index(request):
    all_medicine = Medicine.objects.order_by('id')
    return render(request, 'medicine/index.html', {'all_medicine': all_medicine})

def add(request):
if request.method == 'POST':
    form = MedicineForm(request.POST)
    if form.is_valid():
        new = Medicine()
        new.name = form.cleaned_data['药品名称']
        new.price = form.cleaned_data['药品价格']
        new.number = form.cleaned_data['药品编号']
        new.sort = form.cleaned_data['药品分类']
        new.unit = form.cleaned_data['计价单位']
        new.save()
        return render(request, 'medicine/index.html')
else:
    form = MedicineForm()
return render(request, 'medicine/add.html', {'form': form},)

forms.py

from django import forms


class MedicineForm(forms.Form):
    药品编号 = forms.IntegerField()
    药品名称 = forms.CharField(max_length=100)
    药品价格 = forms.IntegerField()
    jj_choice = {
        '瓶': '瓶',
        '包': '包',
        '盒': '盒',
    }
    计价单位 = forms.ChoiceField(
        choices=jj_choice
    )
    药品分类 = forms.CharField(max_length=100)

add.html:

{% extends 'polls/index.html' %}


{% block medicine_form %}
<form action="http://localhost:8000/medicine/add/" method="post">
    {% csrf_token %}
    {{ form }}
    <input type="submit" value="添加药品" />
</form>


{% endblock %}

追溯:

内部39中的文件“E:\ p \ lib \ site-packages \ django \ core \ handlers \ exception.py”.response = get_response(request)文件“E:\ p \ lib \ site-packages \ django \ core \ handlers \ base.py“在_get_response 187. response = self.process_exception_by_middleware(e,request)文件”E:\ p \ lib \ site-packages \ django \ core \ handlers \ base.py“in _get_response 185. response = wrapped_callback(request,* callback_args,** callback_kwargs)文件“F:\ ddjj \ x \ mysite \ medicine \ views.py”添加25. return render(request,'medicine / add.html',{'form': form},)文件“E:\ p \ lib \ site-packages \ django \ shortcuts.py”在render 30. content = loader.render_to_string(template_name,context,request,using = using)文件“E:\ p \在render_to_string 68中的lib \ site-packages \ django \ template \ loader.py“ . 返回template.render(context,request)文件”E:\ p \ lib \ site-packages \ django \ template \ backends \ django.py“在渲染中返回self.template.render(context)文件“E:\ p \ lib \ site-packages \ django \ template \ base.py” . 返回self._render(context)F _render 199中的ile“E:\ p \ lib \ site-packages \ django \ template \ base.py” . 返回self.nodelist.render(context)文件“E:\ p \ lib \ site-packages \ django \ template \ base.py“在render 994. bit = node.render_annotated(context)文件”E:\ p \ lib \ site-packages \ django \ template \ base.py“中的render_annotated 961. return self.render(context)File在渲染器174.中的“E:\ p \ lib \ site-packages \ django \ template \ loader_tags.py” . 返回compiled_parent._render(上下文)文件“E:\ p \ lib \ site-packages \ django \ template \ base . py“in _render 199. return self.nodelist.render(context)File”E:\ p \ lib \ site-packages \ django \ template \ base.py“in render 994. bit = node.render_annotated(context)File”在render_annotated 961中的E:\ p \ lib \ site-packages \ django \ template \ base.py“ . 返回self.render(上下文)文件”E:\ p \ lib \ site-packages \ django \ template \ loader_tags.py “在渲染70. result = block.nodelist.render(context)文件”E:\ p \ lib \ site-packages \ django \ template \ base.py“in render 994. bit = node.render_annotated(context)File” E:\ p \ LIB \站点包在render_annotated 961中的age \ django \ template \ base.py“ . 在render 1050中返回self.render(context)文件”E:\ p \ lib \ site-packages \ django \ template \ base.py“ . 返回render_value_in_context(输出,context)文件“E:\ p \ lib \ site-packages \ django \ template \ base.py”在render_value_in_context 1028中.value = force_text(value)文件“E:\ p \ lib \ site-packages \ django \ utils \ encoding.py“in force_text 76. s = six.text_type(s)File”E:\ p \ lib \ site-packages \ django \ utils \ html.py“in 391. klass.str = lambda self:mark_safe( klass_str(self))str 123中的文件“E:\ p \ lib \ site-packages \ django \ forms \ forms.py” . 返回self.as_table()文件“E:\ p \ lib \ site-packages \ django \ forms \ forms.py“in as_table 271. errors_on_separate_row = False)html_output 226.'field'中的文件”E:\ p \ lib \ site-packages \ django \ forms \ forms.py“:six.text_type(bf) ,391中的文件“E:\ p \ lib \ site-packages \ django \ utils \ html.py”.klass.str = lambda self:mark_safe(klass_str(self))文件“E:\ p \ lib \ site-在str 43中的packages \ django \ forms \ boundfield.py“返回self.as widget()文件“E:\ p \ lib \ site-packages \ django \ forms \ boundfield.py”in_widget 101. return force_text(widget.render(name,self.value(),attrs = attrs))File“ E:\ p \ lib \ site-packages \ django \ forms \ widgets.py“在渲染537. options = self.render_options([value])文件”E:\ p \ lib \ site-packages \ django \ forms \ widgets.py“在render_options 560. for option_value,option_label in self.choices:Exception Type:ValueError at / medicine / add / Exception Value:没有足够的值来解包(预期2,得到1)

回答(2)

2 years ago

你有

return render(request, 'medicine/add.html', {'form': form},)

你需要

return render(request, 'medicine/add.html', {'form': form})

, 在最后使它成为元组

2 years ago

jj_choice = {
    '瓶': '瓶',
    '包': '包',
    '盒': '盒',
}

应写成,

jj_choice = (
    ('瓶', '瓶'),
    ('包', '包'),
    ('盒', '盒'),
)

键,文本元组 .