【问题标题】:Embedded image in html not displayed from Google App Engine projecthtml 中的嵌入图像未从 Google App Engine 项目中显示
【发布时间】:2015-09-10 03:16:44
【问题描述】:

我想在我的处理程序提供的 html 文件中的表单之前显示一些图像;我已经归结为下面最小的一段代码。

我的图像需要存储在哪里?我需要在app.yaml 中指定什么才能看到它们?目前它们没有显示,大概是因为 GAE 在其他位置查找它们(我将 gif 图像保存在根文件夹中)。

代码如下:

import webapp2

form="""
<img src="./c1.gif"><img src="./r1.gif">
<form action="/test">
   <input name="q">
   <input type="Submit">
</form>
"""

class MainPage(webapp2.RequestHandler):
   def get(self):
      self.response.headers['Content-Type'] = 'text/html'
      self.response.write(form)

class TestHandler(webapp2.RequestHandler):
   def get(self):
      q = self.request.get("q")
      self.response.headers['Content-Type'] = 'text/html'
      self.response.write("Hello, still not working!")

app = webapp2.WSGIApplication([
   ('/', MainPage),
   ('/test', TestHandler)
], debug=True)

App.yaml:

 version: 1
 runtime: python27
 api_version: 1
 threadsafe: true

 handlers:
 - url: /.*
   script: helloworld.app

编辑:用我的 app.yaml 尝试了以下以及其他变体,但没有运气:

version: 1
runtime: python27
api_version: 1
threadsafe: true

handlers:
- url: /images/*.gif
  static_dir: images
- url: /.*
  script: helloworld.app

【问题讨论】:

    标签: python html image google-app-engine


    【解决方案1】:

    您的app.yaml 几乎是正确的,在Static file handlers 中,您将找到配置静态目录处理程序所需的所有信息,但这真的很简单:

    version: 1
    runtime: python27
    api_version: 1
    threadsafe: true
    
    handlers:
    - url: /images
      static_dir: images
    - url: /.*
      script: helloworld.app
    

    然后在html上你需要写完整的路径,从root开始:

    <img src="/images/c1.gif">
    

    当然,这假设您在 app.yaml 旁边有一个 images 文件夹,并且其中包含上述文件。

    请务必阅读链接的文档,因为您应该注意其他细微差别(例如静态目录是从实例外部的 cdn 提供的事实)。

    【讨论】:

    • 感谢@Jaime,这非常有帮助,而不是来自其他答案的 cmets 毫无意义的线程。
    【解决方案2】:

    有很多方法可以做到这一点。

    【讨论】:

    • 我确实查看了您粘贴的文档链接,但我在此基础上尝试了一些东西,但没有任何效果。我的 app.yaml 如下所示,我的图像存储在根目录下的图像文件夹中。我也尝试了一个静态/图像文件夹,但没有运气。版本:1 运行时:python27 api_version:1 线程安全:true 处理程序:- url:/*.gif static_dir:图像 - url:/.* 脚本:helloworld.app
    • 静态文件夹是指静态/图片,所有图片引用都在这个文件夹中搜索?或者它只是根目录下的图像/?我都试过了。 GA引擎看到img src标签到哪里去找图片文件?
    • 取决于您如何请求它,这就是您指定处理程序模式的原因。问题是什么?可以直接打开图片url吗?
    • 是的,如果我将该代码放在 html 文件中,一切都会正确显示。
    • @user3079275 “那个”代码?那么现在一切正常吗?
    猜你喜欢
    • 1970-01-01
    • 2011-08-07
    • 2013-07-30
    • 2020-02-28
    • 1970-01-01
    • 2015-05-26
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    相关资源
    最近更新 更多