使用Django视图来处理blobstore上传 [英] Use Django views for handling blobstore uploads
问题描述
代替使用AppEngine中提供的 BlobstoreUploadHandler
,我更喜欢使用Django视图,所以我可以保留所有的url和查看功能在一起。但是,我无法找到如何获取上传文件的 blob-key
! (如 get_uploads()为上传处理程序) 。我看到 BlobstoreUploadHandler
使用 request.params ,但我不认为可以从Django的请求。
In stead of using the BlobstoreUploadHandler
supplied in AppEngine, I'd prefer to use a Django view, so I can keep all the urls and view functions together. However, I can't find out how to get the blob-key
of the uploaded file! (like get_uploads() does for the upload handler). I saw that the BlobstoreUploadHandler
uses request.params, but I don't think that is available from Django's Request.
def upload_form(request):
upload_url = blobstore.create_upload_url(reverse(upload_blob))
output = '<html><body>'
output += '<form action="%s" method="POST" enctype="multipart/form-data">' % upload_url)
output += ('''Upload File: <input type="file" name="file"><br> <input type="submit"
name="submit" value="Submit"> </form></body></html>''')
def upload_blob(request):
print request
# How to get the 'blob-key' from request?!
当我检查请求对象时,我所得到的是
When I examine the request object, all I get is
<WSGIRequest
GET:<QueryDict: {}>,
POST:<QueryDict: {u'submit': [u'Submit']}>
# And COOKIES, META, etcetera
编辑:Request.FILES
我发现一些信息可以使用Request.FILES来提取,它给出:
Request.FILES
I discovered that some info can be extracted using Request.FILES, which gives:
<MultiValueDict: {u'file': [<InMemoryUploadedFile: my_file (message/external-body)>]}>
但是,我认为blobstore仍然处理文件内容(这就是为什么它说content_type =消息/外部身体?),所以我仍然需要密钥。调用 read() 给出:
However, I assume that the blobstore still handles the file content (is that why it says "content_type=message/external-body"?), so I still need the key somehow. Calling read() gives:
Content-Type: application/octet-stream
MIME-Version: 1.0
Content-Length: 17
Content-MD5: ZmQ3OTJhNjMzNGE0OTAzNGU4NjE5MDNmMGEwNjliMGE=
content-type: application/octet-stream
content-disposition: form-data; name="file"; filename="a1_blob"
X-AppEngine-Upload-Creation: 2012-02-12 22:11:49.643751
所以看起来AppEngine实际上是用这个描述符替换了这个文件的内容,但是AppEngine还是放在哪里呢?
So it looks like AppEngine actually replaced the file content by this descriptor, but still, where does AppEngine put the key?
由于UploadFile对象没有key()方法,我开始怀疑不使用webapp框架时blob-key丢失了。 >
I'm starting to suspect that the blob-key is just lost when not using the webapp framework, since the UploadedFile object has no key() method.
推荐答案
花了我很长时间才找到,但是 content_type:message / external-body
需要额外的参数来查找实际文件,在AppEngine的情况下,这是 blob-key
。然而,Django不支持这些额外的content_type参数,所以在这个过程中确实丢失了。似乎有一个补丁,但我不认为它在AppEngine Django版本。
It took me a long time to find, but the content_type: message/external-body
requires extra parameters, to find the actual file, in AppEngine's case, this is the blob-key
. However, Django doesn't support these extra content_type parameters, so they are indeed lost in the process. There seems to be a patch, but I don't think it's in the AppEngine Django version yet.
https://code.djangoproject.com/ticket/13721
这篇关于使用Django视图来处理blobstore上传的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!