【问题标题】:Cannot get index.html in webapp2 with python27无法使用 python27 在 webapp2 中获取 index.html
【发布时间】:2014-01-10 00:29:22
【问题描述】:

我正在尝试在谷歌应用引擎中从 python2.5 转到 python 2.7。

我就是无法启动 index.html。我得到一个空白网页,仅此而已。 index.html 与 .yaml 和 .py 文件位于同一目录中。

我不知道如何使它工作,因为没有这样的教程或示例或任何告诉你如何呈现 index.html 的东西。

我发现的是写出“Hello World!”的示例。 我找到了一些“几乎有”的例子,但并不完全有。

这里是 app.yaml 和 main.py

application: myapp
version: main
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /(.*\.(gif|png|jif|jpg|jpeg|ico|js|css))
 static_files: \1
 upload: (.*\.(gif|png|jpeg|jpg|ico|js|css))

- url: .*
 script: main.application

.

import os

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

class BaseHandler(webapp2.RequestHandler):
  def render_template(self, filename, **template_args):
     path = os.path.join(os.path.dirname(__file__), 'templates', filename)
     self.response.write(template.render(path, template_args))

class IndexHandler(BaseHandler):
  def get(self):
    self.render_template('index.html', name=self.request.get('name'))

application = webapp2.WSGIApplication([
    ('/', IndexHandler),
], debug=True)

【问题讨论】:

  • 究竟是什么不起作用?
  • 它不加载 index.html

标签: python google-app-engine python-2.7


【解决方案1】:

行内:

     path = os.path.join(os.path.dirname(__file__), 'templates', filename)

您是在告诉您的代码在“模板”目录中查找要呈现的模板,而不是在应用的根目录中。要么使用“模板”目录,要么将其从连接中删除。

【讨论】:

    猜你喜欢
    • 2021-12-26
    • 1970-01-01
    • 2019-06-11
    • 1970-01-01
    • 2012-06-26
    • 1970-01-01
    • 2021-10-21
    • 2013-03-18
    • 2011-10-09
    相关资源
    最近更新 更多