下面是我的代碼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 %}
回溯:
File "E:\p\lib\site-packages\django\core\handlers\exception.py" in inner 39. response = get_response(request)
File "E:\p\lib\site-packages\django\core\handlers\base.py" in _get_response 187. response = self.process_exception_by_middleware(e, request)
File "E:\p\lib\site-packages\django\core\handlers\base.py" in _get_response 185. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "F:\ddjj\x\mysite\medicine\views.py" in add 25. return render(request, 'medicine/add.html', {'form': form},)
File "E:\p\lib\site-packages\django\shortcuts.py" in render 30. content = loader.render_to_string(template_name, context, request, using=using)
File "E:\p\lib\site-packages\django\template\loader.py" in render_to_string 68. return template.render(context, request)
File "E:\p\lib\site-packages\django\template\backends\django.py" in render 66. return self.template.render(context)
File "E:\p\lib\site-packages\django\template\base.py" in render 208. return self._render(context)
File "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 "E:\p\lib\site-packages\django\template\base.py" in render_annotated 961. return self.render(context)
File "E:\p\lib\site-packages\django\template\loader_tags.py" in render 174. return compiled_parent._render(context)
File "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 "E:\p\lib\site-packages\django\template\base.py" in render_annotated 961. return self.render(context)
File "E:\p\lib\site-packages\django\template\loader_tags.py" in render 70. result = block.nodelist.render(context)
File "E:\p\lib\site-packages\django\template\base.py" in render 994. bit = node.render_annotated(context)
File "E:\p\lib\site-packages\django\template\base.py" in render_annotated 961. return self.render(context)
File "E:\p\lib\site-packages\django\template\base.py" in render 1050. return render_value_in_context(output, context)
File "E:\p\lib\site-packages\django\template\base.py" in render_value_in_context 1028. value = force_text(value)
File "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))
File "E:\p\lib\site-packages\django\forms\forms.py" in str 123. return self.as_table()
File "E:\p\lib\site-packages\django\forms\forms.py" in as_table 271. errors_on_separate_row=False)
File "E:\p\lib\site-packages\django\forms\forms.py" in _html_output 226. 'field': six.text_type(bf),
File "E:\p\lib\site-packages\django\utils\html.py" in 391. klass.str = lambda self: mark_safe(klass_str(self))
File "E:\p\lib\site-packages\django\forms\boundfield.py" in str 43. return self.as_widget()
File "E:\p\lib\site-packages\django\forms\boundfield.py" in as_widget 101. return force_text(widget.render(name, self.value(), attrs=attrs))
File "E:\p\lib\site-packages\django\forms\widgets.py" in render 537. options = self.render_options([value])
File "E:\p\lib\site-packages\django\forms\widgets.py" in render_options 560. for option_value, option_label in self.choices:
Exception Type: ValueError at /medicine/add/ Exception Value: not enough values to unpack (expected 2, got 1)
但它不起作用...... – dogewang