本文介绍了如何从blobstore显示图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我试图了解使用Images Python API ,我很困惑如何获得钥匙并显示头像。 文档说明图片处理程序将为图像提供 / img 路径。 我很困惑这个图像处理程序。我在下面评论如何理解它;请改正。感谢: class Image(webapp.RequestHandler): def get(self): #get数据存储#中的图像img_id的关键是img_id的值?它从何而来? #how应用引擎是否知道获取哪个图像的密钥? greeting = db.get(self.request.get(img_id)) #what是greeting.avatar? #is it img_id.avatar? #I假设avatar是指模型中的avatar属性,如果greeting.avatar: self.response.headers ['Content-Type'] =image / png #是否显示头像? #我以为img标签显示的是头像 self.response.out.write(greeting.avatar) else: self.error(404) 非常感谢您的帮助。 UPDATE (re:由Gabi Purcaru回答) 再次感谢您的明确回答。我有一个显示用户注释的查询,如下所示: 查询结果: 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>) 因此,我将该行复制到 MainPage处理程序 self.response.out.write(< div>< img src ='img?img_id =%s'>< / img>%greeting.key()) 并更改 问候语.key() 至 result.key() 我假设现在应该显示用户旁边的化身评论: 获得结果结果: 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>) 但仍不清楚为什么 result .key()是我想要显示的图像的关键? 解决方案 img_id来自url的GET部分(类似于www.example.com/img? img_id = 12312 )。引擎为数据库中的每个模型分配一个新的唯一键。 是模型的头像 img_id 的头像属性。因此,从某种意义上讲,您可以将它想象为 img_id.avatar ,尽管从技术上讲它是不正确的。 不显示头像,它只是返回头像。举个例子,让你更好地理解。当您编写< img src =some_link/> 时,浏览器将查找some_link,并包含该图像。然后浏览器将从文件系统读取图像,并将其返回给浏览器。您的处理程序所做的是更改后端部分,以便Web服务器将返回数据存储中的映像(特别是 avatar 属性),而不是常规文件。浏览器 - 和用户 - 将它看作一个普通的图像。 编辑: / strong> result.key()是数据库自动为您的模型提供的唯一标识符。你需要告诉它你刚刚写的图像处理程序,因为它知道你需要哪个特定的模型头像。你可以通过设置url的 img_id GET变量来实现这一点(你刚刚做过)。 我不是当然,如果你了解整个 .key()的话。让我解释一下: 任何数据库都需要从另一个数据库中识别出一个记录(在我们的例子中是模型)。这就是为什么他们会自动为数据库中插入的每条记录分配一个新的,最重要的唯一标识符(在我们的例子中为键)。您必须为您的处理程序提供模型的关键字,以返回该模型的化身。 让我们以一个真实世界为例:您是一个个体。你的国家唯一标识你的方式是通过某种SSN(社会安全号码)。在我国,它是一个13位数的代码(例如 1024582485008 )。如果我想获得我的驾驶执照,我将不得不提供我的名字,但这还不够 - 我不是我国唯一的Gabi Purcaru。我还必须提供我的SSN,它将确切地说明我是谁。如果我们做一个类比,你必须提供模型的SSN(即键)给处理程序,以便它知道哪个模型从数据库中获得并返回它的头像。 > I am trying to understand the documentation Using the Images Python API and I am confused about how to get the key and display the avatar.Documentation says that the Image handler will serve the image off the /img path.I am confused about what this Image handler does. I comment below how understand it; please correct. Thanks: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)Many thanks for your help.UPDATE (re: answer by Gabi Purcaru)Thanks again for the clear answer. I have a query that displays the user comments like this: 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>")so, I copy the line with the image tag from the MainPage handlerself.response.out.write("<div><img src='img?img_id=%s'></img>" % greeting.key())and changegreeting.key()toresult.key()I assume that, this should now display the avatar next to the user comment: 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>")but still not clear why result.key() is the key of the image I want to display? 解决方案"img_id" comes from the GET part of the url (something like "www.example.com/img?img_id=12312"). The engine assigns a new unique key for every model in the database.greeting.avatar is the avatar property of the model with the key img_id. So, in some sense, you can think of it like img_id.avatar, although technically it is not correct.that does not display the avatar, it is just returning the avatar. Let's take a usual image for example for you to understand better. When you write <img src="some_link" />, the browser will look for "some_link", and include that image. The browser will then read the image from the filesystem, and return it to the browser. What your handler does is changing the backend part, so that the webserver will return the image from the datastore (specifically that avatar property) instead of a regular file. The browser -- and the user -- will see it as a regular image.EDIT:result.key() is the unique identifier that the database automatically gave your model. You need to "tell it" to the image handler you just wrote, for it to know which particular model avatar you need. You do that by setting the img_id GET variable for the url (which you just did).I am not sure if you understand the whole .key() thing. Let me explain that:Any database needs to identify one record from another (model in our case). This is why they will automatically assign a new, and most importantly unique identifier (key in our case) to each and every record inserted in the database. You will have to provide the model's key for your handler to return the avatar of that model.Let's take a real-world example: you are an individual amongst many. The way that your country uniquely identifies you is by some kind of SSN (Social Security Number). In my country it is a 13-digits code (e.g. 1024582485008). If I want to get my driver licence, I will have to provide my name, but that is not enough -- I'm not the only Gabi Purcaru in my country. I will also have to provide my SSN, which will tell exactly who I am. If we make an analogy, you have to provide the model's "SSN" (i.e. key) to the handler, so that it will know which model to get from the database and return it's avatar. 这篇关于如何从blobstore显示图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 22:52