我有一个 Django 网站,在其中一个页面上,我正在尝试使用表单上传一些图像。
当我单击“浏览”按钮选择要上传的文件时,会打开一个对话框,然后我浏览到该文件并单击“确定”。所选文件的名称随后会显示在“浏览”按钮旁边。
但是,当我单击“提交”按钮提交表单(并将附加的图像上传到该对象的数据库条目)时,控制台会显示以下输出:
Exception thrown in try in if drawing_formset:
u'drawings-0-id' (<class 'django.utils.datastructures.MultiValueDictKeyError'>)
Exception thrown in try in if budget_formset:
ManagementForm data is missing or has been tampered with (<class 'django.core.exceptions.ValidationError'>)
此异常由view
我用来将图像附加到表单的处理:
def upload_budget_pdfs(request, project_id):
project = Project.objects.get(id=project_id)
print("Value of project in 'upload_budget_pdfs()': ", project)
if request.method == 'POST':
presentations = project.budget_versions.select_related('meeting').prefetch_related('budget_items', 'cci_items', 'presenters').filter(version_number__isnull=False).annotate(vn=F('version_number') * -1).order_by('presentation_date', 'created', '-vn')
print("Value of presentations in 'upload_budget_pdfs()': ", presentations)
drawing_formset = DrawingUploadFormset(request.POST, request.FILES, prefix="drawings", queryset=Drawing.objects.filter(budget__in=presentations).order_by('budget__presentation_date', 'budget__created'))
if drawing_formset:
print "Before", [b.id for b in project.budget_versions.all()]
# Surround for loop with a try-catch to see what's going on...
try:
for drawing_form in drawing_formset: #ERF(24/01/2017 @ 1610) This line is what's causing the MultiValueDictKeyError
print 'drawing for loop entered in upload_budget_pdfs() - line 1034 '
if drawing_form.instance.budget:
print 'if statement entered - line 1036 '
print 'Instance: ', drawing_form.instance.budget
drawing = drawing_form.save(commit=False)
drawing.budget = drawing_form.instance.budget
drawing.save()
print "drawing.save() called in upload_budget_pdfs() - line 1055 "
print drawing, [b.id for b in project.budget_versions.all()]
except Exception as e:
print 'Exception thrown in try in if drawing_formset: '
print '%s (%s)' % (e.message, type(e))
else:
print("Drawing formset not valid. ", drawing_formset.errors)
budget_formset = DrawingUploadFormset(request.POST, request.FILES, prefix="Drawings")
budget_pdf_formset = BudgetPresentationFormset(request.POST, request.FILES, instance=project, prefix="budgetPDF")
if budget_formset:
try:
budgetSaved = False
for budget_form in budget_formset:
if budget_form:
if budgetSaved == False:
print 'if statement entered - line 1081 '
budget = budget_form.save(commit=False)
budget.save()
print "budget.save() called in upload_budget_pdfs() - line 1097 "
budgetSaved = True
except Exception as e:
print 'Exception thrown in try in if budget_formset: '
print '%s (%s)' % (e.message, type(e))
else:
print("Budget formset not valid. ", budget_formset.errors)
return HttpResponseRedirect(reverse('projects:concept', args=[project_id]))
在查看了可能导致引发此异常的原因后,我在以下位置找到了答案:ManagementForm data is missing or has been tampered with,这似乎表明我需要使用
{{ my_formset.management_form }}
在我的模板中...
但是,我已经management_form
在我的模板中使用了:
{% with drawing_form=drawing_formset|getval:forloop.counter0 %}
{# budget_pdf_form=budget_pdf_formset|getval:forloop.counter0 #}
<tr>
{% if not forloop.last %}
<td colspan="3"><label>Drawings</label></td>
{% endif %}
{% for d_field in drawing_form.visible_fields %}
{% if drawing_form.instance.pdf %}
<td colspan="3" class="center">
<a class="button file-download pdf" href="{% url 'costing:pdf_open' presentation_form.instance.id %}?pdf=drawings" target="_blank"></a><a class="pdf-clear" data-view-url="{% url 'costing:pdf_clear' presentation_form.instance.id %}?pdf=drawings"><img class="icon m-l-sm m-b-md" src="{% static "img/bin.png" %}"></a>
<!-- Need a hidden field to actually hold the file that's uploaded to the form -->
<input type="hidden" name = "conceptDrawing" value="{{d_field.title}}">
</td>
{% else %}
<td colspan="3">{{d_field}}</td>
{% for d_hidden in drawing_form.hidden_fields %}
<!--td class="hidden">{{d_hidden}}</td-->
<td class="hidden">
{{budget_formset.as_table}}
{{budget_formset.management_form}}
</td>
{% endfor %}
{% endif %}
{% endfor %}
<tr>
<td colspan="1" class="p-t-md"></td>
<td colspan="4" class="p-t-md"><input type="submit" value="upload"></td>
<td colspan="1" class="p-t-md"></td>
</tr>
</tr>
{% endwith %}
{# Add a similar with statement for budgetPDF form #}
{% with budget_pdf_form=budget_pdf_formset|getval:forloop.counter0 %}
<tr>
{% if not forloop.last %}
<td colspan="3"><label>Budget PDF package</label></td>
{% endif %}
</tr>
<tr>
{% for p_field in budget_pdf_form.visible_fields %}
{% if budget_pdf_form.instance.pdf %}
<td colspan="3" class="center">
<a class="button file-download pdf" href="{% url 'costing:pdf_open' presentation_form.instance.id %}?pdf=budgetPDF" target="_blank"></a><a class="pdf-clear" data-view-url="{% url 'costing:pdf_clear' presentation_form.instance.id %}?pdf=budgetPDF"><img class="icon m-l-sm m-b-md" src="{% static "img/bin.png" %}"></a>
<input type="hidden" name = "conceptBudgetPDF" value="{{p_field.title}}">
<!--a class="button file-download pdf" href="{% url 'costing:pdf_open' presentation_form.instance.id %}?pdf=drawings" target="_blank"></a><a class="pdf-clear" data-view-url="{% url 'costing:pdf_clear' presentation_form.instance.id %}?pdf=drawings"><img class="icon m-l-sm m-b-md" src="{% static "img/bin.png" %}"></a-->
<!-- Need a hidden field to actually hold the file that's uploaded to the form -->
<!--input type="hidden" name = "conceptBudgetPDF" value="{{p_field.title}}"-->
</td>
<td>Some text here</td>
{% else %}
<td colspan="3">{{p_field}}</td>
{%for p_hidden in budget_pdf_form.hidden_fields %}
<td class="hidden">{{p_hidden}}</td>
{{budget_pdf_formset.as_table}}
{{budget_pdf_formset.management_form}}
</td>
{% endfor %}
<td>Other text here</td>
{% endif %}
{% endfor %}
<tr>
<td colspan="1" class="p-t-md"></td>
<td colspan="4" class="p-t-md"><input type="submit" value="upload"></td>
<td colspan="1" class="p-t-md"></td>
</tr>
</tr>
{% endwith %}
我不明白为什么我会遇到这些异常......这MultiValueDictKeyError
似乎表明我正在返回/使用几个应该只是一个的对象,我的猜测是当我分配值时会发生这种情况of drawing_formset
,即它被赋予了 a 的值queryset
,因此这将是多个对象。
但是,在我的控制台中显示的输出将表明仅当我尝试遍历已分配给变量的对象列表时才抛出此异常drawing_formset
,即在 中的try
块中view
,所以这肯定表明我我期望将几个对象分配给变量drawing_formset
,并且我想遍历它们...?
如何解决这些异常?