Django 表单集无效——为什么不呢?

Django formset is not valid- why not?

我正在尝试使用一种表单允许用户将图像上传到存储在我的 Django 项目的数据库中的项目,但是,我目前收到的控制台输出告诉我 formset 我' m 使用无效...

我试图用来将图像上传到项目的视图定义为:

def upload_budget_pdfs(request, project_id):
    project = Project.objects.get(id=project_id)
    print("Value of project in 'upload_budget_pdfs()': ", project)

    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(prefix="drawings", queryset=Drawing.objects.filter(budget__in=presentations).order_by('budget__presentation_date', 'budget__created'))
    print("Value of drawing_formset in 'upload_budget_pdfs()': ", drawing_formset)

    if drawing_formset.is_valid():
        print 'Saving drawing_formset'
        print "Before", [b.id for b in project.budget_versions.all()]
        for drawing_form in drawing_formset:
            if drawing_form.instance.budget:
                print 'Instance', drawing_form.instance.budget
                drawing = drawing_form.save(commit=False)
                drawing.budget = drawing_form.instance.budget
                drawing.save()
            print drawing, [b.id for b in project.budget_versions.all()]
    else: print 'Drawing formset not valid.',drawing_formset.errors

    budget_formset = BudgetPresentationFormset(request.POST, request.FILES, instance=project, prefix="presentations")

    if budget_formset.is_valid() and budget_formset.has_changed():
        updated_budget_presentations = budget_formset.save()
    elif budget_formset.has_changed(): print 'Budget formset not valid.',budget_formset.errors



    return HttpResponseRedirect(reverse('projects:concept', args=[project_id]))

调用此视图时我得到的控制台输出是:

("Value of project in 'upload_budget_pdfs()': ", < Project: Test 1 >)

("Value of presentations in 'upload_budget_pdfs()': ", [< Budget: Test 1: Version -1 >, < Budget: Test 1: Version -1 >, < Budget: Test 1: Version 0 >, < Budget: Test 1: Version 0 >, , , , , , , , , , , , , , , , , '...(remaining elements truncated)...'])

("Value of drawing_formset in 'upload_budget_pdfs()': ", < django.forms.formsets.DrawingFormFormSet object at 0x10cee3790 >)

Drawing formset not valid. []

所以很明显 else 语句在我看来就是 运行 这里的内容,但我不确定为什么,因为控制台清楚地表明变量都包含正确的数据...

我在这里初始化 drawing_formset 变量的方式有什么问题?我传递给 DrawingUploadFormset(...)queryset 是否正确? is_valid() 实际检查什么?

编辑

我在已接受的答案中进行了更改,因此我的 drawing_formset 声明现在是:

        drawing_formset = DrawingUploadFormset(request.POST, request.FILES, prefix="drawings", queryset=Drawing.objects.filter(budget__in=presentations).order_by('budget__presentation_date', 'budget__created'))

但我现在似乎在线上收到 'Internal Server Error' 和 MultiValueDictKeyError:

for drawing_form in drawing_formset: #This line is what's causing the MultiValueDictKeyError

我不明白这是为什么?有什么建议吗?

您的表单集不包含任何数据。您需要传递 request.POSTrequest.FILES 作为第一个和第二个参数(或作为 datafiles 关键字参数),但前提是它是实际的表单提交。

如果没有数据传递到表单或表单集中,它被认为是未绑定的,并且将 return False 不检查错误。

通常的模式是在 request.method == 'POST' 时传递它们,然后验证表单集:

def upload_budget_pdfs(request, project_id):
    ...
    if request.method == 'POST':
        drawing_formset = DrawingUploadFormset(request.POST, request.FILES, prefix='drawings', queryset=...)
        if drawing_formset.is_valid():
            # save formset
            return HttpResponseRedirect(...)
    else:
        drawing_formset = DrawingUploadFormset(prefix='drawings', queryset=...)
    return render(...)  # Render formset

这将在 GET 上显示一个空白表单,一个 filled-in 表单在提交不成功时显示错误消息,它会保存表单集并在成功提交时重定向。