简体   繁体   English

将blob参数传递给Django

[英]passing blob parameter to django

I keep my images in the DB as blobs: 我将图像作为斑点保存在数据库中:

class MyClass(db.Model):
    icon=db.BlobProperty()

Now, I want to send the blob to my HTML like this : lets say I have myClass as an instance of MyClass 现在,我想像这样将Blob发送到我的HTML:可以说我将myClass作为MyClass的实例。

result = """<div img_attr=%s> Bla Bla </div>""" % (myClass.icon)

Some how it doesn't work. 一些不起作用的方法。 Do you have any idea? 你有什么主意吗?

You cannot just dump raw image data into your html page. 您不能只将原始图像数据转储到html页面中。 You need to do this in two pieces: 您需要分两步执行此操作:

In your html , you need to refer to an image file: 在您的html中 ,您需要引用一个图像文件:

result = "<div>"
         "<img src='{0}' />"
         "</div>"
         .format(MYSITE + MYIMAGEDIR + myClass.name)

Your browser reads the html page, finds out you want to include an image, and goes looking for the image file to include - so it asks your site for something like http://www.myexample.com/images/icon01.jpg 您的浏览器会读取html页面,找到要包含的图像,然后继续寻找要包含的图像文件-因此它会向您的网站询问类似http://www.myexample.com/images/icon01.jpg的内容

Now, separately, you respond to this second request with the image content, as @anand has shown. 现在,您分别使用图像内容响应第二个请求 ,如@anand所示。

Your code suggests that you are working on Google application engine with Django. 您的代码表明您正在使用Django开发Google应用程序引擎。

You just need to query the image in your view and return it as http response. 您只需要在视图中查询图像并将其作为http响应返回即可。

image = myclassObject.icon 图片= myclassObject.icon

response = HttpResponse(image) 响应= HttpResponse(图像)

response['Content-Type'] = 'image/jpg' response ['Content-Type'] ='image / jpg'

return response 返回响应

The value stored in the the data store, and returned by appengine with a db.BlobProperty is not the actual bytes of the blob, but rather a BlobKey that is used to reference it. 存储在数据存储区中的值,由appengine与db.BlobProperty返回,该值不是Blob的实际字节,而是用于引用它的BlobKey。 There are two ways you can use that key. 您可以通过两种方式使用该密钥。 You can create a BlobReader to load the bytes of the blob from the BlobStore into your app, or you can craft a response with ServeHandler.send_blob to transfer those bytes to the client. 您可以创建一个BlobReader,以将Blob存储区中的Blob字节加载到您的应用中,也可以使用ServeHandler.send_blob做出响应,以将这些字节传输到客户端。

Doing the second one in Django is a bit of a headache, because ServeHandler doesn't really fit in well with the Django request processing stack. 在Django中进行第二个操作有些令人头疼,因为ServeHandler确实不适用于Django请求处理堆栈。 Here's a view that will do it for you without too much trouble: 这是一种可以为您轻松完成的工作:

def get_image_data(request, key, filename):
  "serve original uploaded image"

  # verify the users' ability to get the requested image
  key = urllib.unquote(key)
  img = _load_metadata(request, key)
  blob = img.data;
  blobkey = blob.key()

  # and tell google to serve it
  response = http.HttpResponse(
      content='',
      content_type=blob.content_type)
  response['X-AppEngine-BlobKey'] = str(blobkey)
  return response

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM