将blob参数传递给django [英] passing blob parameter to django

查看:169
本文介绍了将blob参数传递给django的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将图像作为斑点保存在数据库中

I keep my images in the DB as blobs:

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

现在,我想像这样将Blob发送到我的HTML: 可以说我有myClass作为MyClass

Now, I want to send the blob to my HTML like this : lets say I have myClass as an instance of MyClass

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

有些不起作用.你有什么主意吗?

Some how it doesn't work. Do you have any idea?

推荐答案

您不能仅将原始图像数据转储到html页面中.您需要将其分为两部分:

You cannot just dump raw image data into your html page. You need to do this in two pieces:

在您的html 中,您需要引用图像文件:

In your html, you need to refer to an image file:

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

您的浏览器阅读html页面,发现要包含图像,然后继续寻找要包含的图像文件-因此它会向您的网站询问类似

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

现在,您分别使用图像内容来响应此第二次请求,如@anand所示.

Now, separately, you respond to this second request with the image content, as @anand has shown.

这篇关于将blob参数传递给django的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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