Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload a file with django (python) and s3?

I'm looking for a way to upload a file to s3. I am using django. I am currently using amazon's python library for uploading along with the following code:

View:

def submitpicture(request):
    fuser = request.session["login"]

    copied_data = request.POST.copy()
    copied_data.update(request.FILES)
    content_type = copied_data['file'].get('content-type')
    ffile = copied_data['file']['content']
    key = '%s-%s' % (fuser, ''.join(copied_data['file']['filename'].split(' ')))
    site_s3.save_s3_data(key, ffile, content_type)

Template:

<form action="/submitpicture/" method="POST">
    <input type="file" id="file" name="file" />
    <input type="submit" value="submit" />
</form>

However, when I actually try to run it i get the following error:

"Key 'file' not found in <QueryDict: {}>"
#MultiValueDictKeyError

I really don't see what I'm doing wrong. Can someone point me in the right direction?

Edit: Just in case someone wonders, I am planning on adding some validation after I get the actual upload working.

like image 588
rksprst Avatar asked Nov 26 '08 06:11

rksprst


1 Answers

You will have to provide the enctype attribute to the FORM element (I've been bitten by this before). For example, your FORM tag should look like:

<form action="/submitpicture/" method="POST" enctype="multipart/form-data" >

Without the enctype, you will find yourself with an empty request.FILES.

like image 200
ayaz Avatar answered Oct 06 '22 22:10

ayaz