【问题标题】:Page isn't always rendered页面并不总是呈现
【发布时间】:2009-06-12 04:35:23
【问题描述】:

在 Google App Engine 中,我有以下代码,它显示了一个简单的 HTML 页面。

import os

from google.appengine.ext.webapp import template
from google.appengine.ext import webapp

class IndexHandler(webapp.RequestHandler):
  def get(self):
    template_values = { }

    path = os.path.join(os.path.dirname(__file__), '../templates/index.html')
    self.response.out.write(template.render(path, template_values))

问题是页面并不总是被渲染。 index.html 是一个简单的“Hello World!”。几次页面刷新后,页面正确显示(即找到了 index.html 文件......)。我试图在最后调用flush,但它没有帮助。我可以用 SDK 和他们的服务器重现这个。

我错过了什么吗?有人知道发生了什么吗?

谢谢

【问题讨论】:

    标签: python google-app-engine


    【解决方案1】:

    您的处理程序脚本(app.yaml 引用的那个)有一个 main() 函数,但最后需要此节:

    if __name__ == '__main__':
      main()
    

    发生的情况是,当您的脚本第一次在给定的解释器中运行时,它会解释您的主脚本,它什么都不做(因此返回空白响应)。在随后的调用中,解释器只需执行您的 main() (记录的优化),它会按预期生成页面。添加上面的节也会导致它在初始导入时执行 main。

    【讨论】:

      【解决方案2】:

      无法重现——目录更改为./templates(在我的设置中没有../templates),并添加了通常的main函数,并且这个脚本在app.yaml中分配给了一些任意的URL,每次都成功服务“Hello World”。猜猜我们需要更多信息来提供帮助——记录条目(可能在此处添加 logging.info 调用?)、app.yamlmain 在哪里等等...

      【讨论】:

        猜你喜欢
        • 2021-04-20
        • 2016-02-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-05-30
        相关资源
        最近更新 更多