I have a ‘ProjectRecord’ model with several filefields:
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)
my upload handler is very basic:
def handle_uploaded_file(file):
destination = open('tmp.pdf', 'wb+')
for chunk in file.chunks():
destination.write(chunk)
destination.close()
and my view function is as follows:
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})
the existing code works only so long as I have a single file request:
handle_uploaded_file(request.FILES['upload_template1'])
as soon as I add multiple file requests (like the former view function), I get key errors.
Is what I’m trying to accomplish possible, and can someone help me with the view function?
this is a condesed version of the generated source
<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>
request.FILESsimply has no key:('upload_template1', 'upload_template2', 'upload_template3')It’s
request.FILES['upload_template1'], request.FILES['upload_template2'], request.FILES['upload_template3']This would illustrate your problem: