Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何显示blobstore中的图像?_Python_Google App Engine_Blobstore - Fatal编程技术网

Python 如何显示blobstore中的图像?

Python 如何显示blobstore中的图像?,python,google-app-engine,blobstore,Python,Google App Engine,Blobstore,我试图理解文档,但我对如何获取密钥和显示化身感到困惑 文档说明,Image处理程序将在/img路径之外提供图像 我对这个图像处理程序的功能感到困惑。我在下面评论如何理解它;请更正。谢谢: class Image (webapp.RequestHandler): def get(self): #get the key of the image "img_id" from datastore #what is the value of "img_id"? Wh

我试图理解文档,但我对如何获取密钥和显示化身感到困惑

文档说明,
Image
处理程序将在
/img
路径之外提供图像

我对这个
图像
处理程序的功能感到困惑。我在下面评论如何理解它;请更正。谢谢:

class Image (webapp.RequestHandler):
    def get(self):
        #get the key of the image "img_id" from datastore
        #what is the value of "img_id"? Where does it come from?
        #how does the app engine know to get what key for which image?
        greeting = db.get(self.request.get("img_id"))
        #what is greeting.avatar?
        #is it img_id.avatar ?
        #I assume "avatar" refers to the "avatar" property in the model
        if greeting.avatar:
            self.response.headers['Content-Type'] = "image/png"
            #does this display the avatar?
            #I thought the img tag displayed the avatar
            self.response.out.write(greeting.avatar)
       else:
          self.error(404)
非常感谢你的帮助


更新(回复:Gabi Purcaru的回答)

再次感谢你的明确回答。我有一个显示用户评论的查询,如下所示:

    for result in results:
        self.response.out.write("<li>")
        self.response.out.write("<b>%s</b> %s " % (result.userName, result.userLatestComment))
        self.response.out.write("</li>")
    self.response.out.write("</ol></body></html>")

我假设,现在应该在用户评论旁边显示化身:

    for result in results:
        self.response.out.write("<li>")
        self.response.out.write("<b>%s</b> %s " % (result.userName, result.userLatestComment))
        self.response.out.write("<div><img src='img?img_id=%s'></img>" % result.key())
        self.response.out.write("</li>")
    self.response.out.write("</ol></body></html>")
对于结果中的结果:
self.response.out.write(“
  • ”) self.response.out.write(“%s%s”%(result.userName,result.userLatestComment)) self.response.out.write(“%result.key()) self.response.out.write(“
  • ”) self.response.out.write(“”)
    但是仍然不清楚为什么
    result.key()
    是我要显示的图像的键

  • “img_id”
    来自url的GET部分(类似于“www.example.com/img?img_id=12312”)。引擎为数据库中的每个模型分配一个新的唯一密钥

  • greeting.avatar
    是带有键
    img\u id
    的模型的化身属性。因此,在某种意义上,你可以把它想象成是
    img\u id.avatar
    ,尽管从技术上讲它是不正确的

  • 它不显示化身,只是返回化身。让我们以一个普通的形象为例,让你更好地理解。编写
    时,浏览器将查找
    “某些链接”
    ,并包含该图像。然后,浏览器将从文件系统读取图像,并将其返回到浏览器。处理程序所做的是更改后端部分,以便Web服务器将从数据存储返回图像(特别是
    avatar
    属性),而不是常规文件。浏览器——以及用户——将其视为常规图像

  • 编辑:
    result.key()
    是数据库自动为您的模型提供的唯一标识符。你需要将它“告诉”给你刚刚编写的图像处理程序,让它知道你需要哪种型号的头像。您可以通过为url设置
    img\u id
    GET变量来实现这一点(您刚刚做了)

    我不确定你是否理解整件事。让我解释一下:

    任何数据库都需要从另一个记录中识别一个记录(在本例中为模型)。这就是为什么他们会自动为数据库中插入的每条记录分配一个新的、最重要的唯一的标识符(在本例中为键)。您必须提供模型的密钥,以便您的处理程序返回该模型的化身


    让我们举一个现实世界的例子:你是众多人中的一员。您的国家通过某种SSN(社会保险号码)唯一地识别您。在我的国家,它是一个13位数的代码(例如,
    1024582485008
    )。如果我想获得驾驶执照,我必须提供我的姓名,但这还不够——我不是我国唯一的加比·珀卡鲁。我还必须提供我的SSN,它将确切地告诉我是谁。如果我们进行类比,您必须向处理程序提供模型的“SSN”(即密钥),以便它知道从数据库中获取哪个模型并返回其化身。

    img_id的值是数据存储密钥,用于指定特定图像对象在数据存储中的精确位置。使用此键执行get将检索问候语(如果存在)。@kevpie:谢谢。应用程序引擎如何知道该行中要获取哪个
    img\u id
    greeting=db.get(self.request.get(“img\u id”)
    ?这不是blobstore。这是数据存储中的一个blob。你需要仔细阅读关于Blobstore的文档。恐怕你混淆了这两个概念。谢谢你的回答。这澄清了很多;但是我仍然不清楚
    db.get(self.request.get(“img_id”)
    如何获取我们想要获取的化身的
    img_id
    。我没有看到任何说明说“获取与特定用户关联的化身相关联的
    img_id
    ”再次感谢。我添加了一个更新。请让我知道您的想法。谢谢。@Zeynel,结果来自对数据存储中实体的查询。每个实体的键在html中作为img src url发出。当浏览器尝试加载该img src时,它会用键点击图像处理程序。该键在db.get中使用,然后从e blob属性在contentType头之后返回。这有帮助吗?@kevpie:但我不明白的是,查询结果返回的实体不是图像;或者
    result.avatar
    。但是在任何情况下,处理程序
    AvatarSave
    都没有将图像写入blobstore。你有什么建议吗?谢谢@Zeynel,这不是使用blobstore。这是数据存储中不明确的事情之一。blob属性不是blobstore对象。此示例仅使用普通blob属性。如果要利用picasa映像服务基础架构或存储大型二进制对象。Blobstore不是数据存储BlobProperty。
    greeting.key()
    
    result.key()
    
        for result in results:
            self.response.out.write("<li>")
            self.response.out.write("<b>%s</b> %s " % (result.userName, result.userLatestComment))
            self.response.out.write("<div><img src='img?img_id=%s'></img>" % result.key())
            self.response.out.write("</li>")
        self.response.out.write("</ol></body></html>")