我有一个带有多个文件字段的'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>
答案 0 :(得分:0)
request.FILES
根本没有密钥:('upload_template1', 'upload_template2', 'upload_template3')
这是request.FILES['upload_template1'], request.FILES['upload_template2'], request.FILES['upload_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