Django 同一型号内的多个文件上载字段

Django 同一型号内的多个文件上载字段,django,django-views,Django,Django Views,我有一个带有几个文件字段的“ProjectRecord”模型: class ProjectRecord(models.Model): prjname = models.CharField(max_length=200, unique=True) agencyID = models.CharField(max_length=30, unique=True, blank=True, null=True) client = models.CharField(max_length=50, choices

我有一个带有几个文件字段的“ProjectRecord”模型:

class ProjectRecord(models.Model):
prjname = models.CharField(max_length=200, unique=True)
agencyID = models.CharField(max_length=30, unique=True, blank=True, null=True)
client = models.CharField(max_length=50, choices=CLIENT_CHOICES)
...
upload_template1 = models.FileField(max_length=100, upload_to="media/%Y%m%d", blank=True, null=True)
    upload_template2 = models.FileField(max_length=100, upload_to="media/%Y%m%d", blank=True, null=True)
    upload_template3 = models.FileField(max_length=100, upload_to="media/%Y%m%d", blank=True, null=True)
我的上传处理程序非常基本:

def handle_uploaded_file(file): 
     destination = open('tmp.pdf', 'wb+') 
     for chunk in file.chunks(): 
             destination.write(chunk) 
             destination.close()
我的查看功能如下:

def edit_project(request, agencyID):
    if request.method == 'POST':
            a=ProjectRecord.objects.get(agencyID=agencyID)
            form = RecordForm(request.POST, request.FILES, instance=a)
            if form.is_valid():
                handle_uploaded_file(request.FILES['upload_template1', 'upload_template2', 'upload_template3' ])
                form.save()
                return HttpResponseRedirect('login.html')
    else:
            a=ProjectRecord.objects.get(agencyID=agencyID)
            form = RecordForm(instance=a)
    return render_to_response('portalproduction/production.html', {'form': form})
现有代码仅在我有一个文件请求时有效:

handle_uploaded_file(request.FILES['upload_template1'])
只要添加多个文件请求(如前一个视图函数),就会出现关键错误。 我想完成的是可能的吗?有人能帮我完成查看功能吗

这是生成源的压缩版本

<div id="formshell" class="clearfix">
<form action="." method="POST" enctype="multipart/form-data">

    <div id="" class="component_stage clearfix">

        <div class="component_wrapper edit cw1">
            <table>
                <tr><td><input id="id_sellnumber1" type="text" name="sellnumber1" value="01" maxlength="30" /></td><td><input id="id_format1" type="text" name="format1" value="Inline Brochure" maxlength="64" /></td></tr>
                <tr><td colspan="2"><input id="id_componentname1" type="text" name="componentname1" value="PA-to-SC_Redhead Brochure" maxlength="200" /></td></tr>
                <tr><td colspan="2"><input type="file" name="upload_template1" id="id_upload_template1" /></td></tr>
            </table>

        </div> 
        <div class="component_wrapper edit cw2">
            <table>
                <tr><td><input id="id_sellnumber2" type="text" name="sellnumber2" value="02" maxlength="30" /></td><td><input id="id_format2" type="text" name="format2" value="OE" maxlength="64" /></td></tr>
                <tr><td colspan="2"><input id="id_componentname2" type="text" name="componentname2" value="PA-to-SC_RedheadOE" maxlength="200" /></td></tr>
                <tr><td colspan="2"><input type="file" name="upload_template2" id="id_upload_template2" /></td></tr>
            </table>
        </div>
        <div class="component_wrapper edit cw3" style="margin-right:0;">
            <table>
                <tr><td><input id="id_sellnumber3" type="text" name="sellnumber3" maxlength="30" /></td><td><input id="id_format3" type="text" name="format3" maxlength="64" /></td></tr>
                <tr><td colspan="2"><input id="id_componentname3" type="text" name="componentname3" maxlength="200" /></td></tr>
                <tr><td colspan="2"><input type="file" name="upload_template3" id="id_upload_template3" /></td></tr>
            </table>
        </div>



    <div id=""><input type='file' name='file' id='file'/><input type="submit" value="save record" /></div>

</form>

请求。文件
根本没有键:
('upload\u template1'、'upload\u template2'、'upload\u template3')

这是
request.FILES['upload\u template1'],request.FILES['upload\u template2'],request.FILES['upload\u template3']

handle_uploaded_file(request.FILES['upload_template1'])
handle_uploaded_file(request.FILES['upload_template2'])
#... so on.
这将说明您的问题:

dic = {'key1':'value1', 'key2': 'value2'}
print dic['key1', 'key2']
# KeyError

dic['key1', 'key2'] = 'value3'
print dic['key1', 'key2']
# out: value3

这似乎也不起作用。代码生成的多值dictKeyError键“upload_template2”在中找不到,那么您不会发布第二个
是的,它就在那里。框架自动生成type=file作为FileField的属性,name=始终使用该字段的名称。@kjarsenal是否在表单标记中?我们能看到生成的HTML吗?如果你的浏览器只发送一个文件而不发送另一个文件,那就很奇怪了。经过进一步测试,代码似乎确实有效,但只要所有上传字段都附加了文件。如果我只上传一两条,然后尝试保存记录,它会抛出错误。表单在管理员中按预期工作。您的html模板中是否有
enctype=“multipart/form data
?如果没有,请发布表单代码