如何上传使用Django(蟒蛇)和S3的文件? [英] How to upload a file with django (python) and s3?

查看:211
本文介绍了如何上传使用Django(蟒蛇)和S3的文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在寻找一种方式将文件上载到S3。我使用的Django的。我目前正在使用亚马逊的python的库以及以下code上传:

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:

查看:

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)

模板:

<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?

编辑:万一有人想知道,我就加入了一些验证计划后,我得到了实际的上传工作。

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

推荐答案

您必须提供的enctype属性的FORM元素(我一直咬这个之前)。例如,您的FORM标记应该是这样的:

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" >

如果没有加密类型,你会发现自己有一个空的request.FILES。

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

这篇关于如何上传使用Django(蟒蛇)和S3的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆