【问题标题】:Python 2.7 GAE app.yaml Getting 404 ErrorPython 2.7 GAE app.yaml 出现 404 错误
【发布时间】:2012-11-13 09:07:11
【问题描述】:

下面是我的app.yaml 文件的代码。如果我去localhost:8080/ 我的index.app 加载正确。如果我转到localhost:8080/index.html,我会收到 404 错误。如果我转到任何其他页面,例如localhost:8080/xxxxnot_found.app 会正确加载。为什么我在 /index\.html 案例中收到 404 错误?

谢谢!

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

handlers:
- url: /index\.html
  script: index.app

- url: /
  script: index.app

- url: /assets
  static_dir: assets

- url: /*
  script: not_found.app

libraries:
- name: jinja2
  version: latest

index.py 中的代码

类 MainPage(webapp2.RequestHandler):

def get(self):

模板 = jinja_environment.get_template('index.html')

self.response.out.write(template.render(template_values))

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

修复位于粗体文本中!

【问题讨论】:

  • 你为什么要转义'.'?
  • 我以为用的是正则表达式引用,所以需要转义句号来抑制特殊含义。
  • index.app 中的处理程序是什么样的?
  • 对于 jinja2,应该从代码中引用 html,不是吗? "jinja.render_template("index.html")"

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


【解决方案1】:

看起来index 中的app 变量没有index.html 的处理程序。例如:

app = webapp2.WSGIApplication([('/', MainPage)])

如果您的应用程序被路由到index,它将查看定义的处理程序并尝试找到与/index.html 的匹配项。在这个例子中,如果你去/,它会正常工作,因为该处理程序已定义;但是如果你去index.html,GAE 不知道要调用哪个类,因此它返回一个 404。作为一个简单的测试,试试

app = webapp2.WSGIApplication([
    ('/', MainPage),
    ('/index\.html', MainPage)
])

由于这表面上是任何输入index.htmlindex. 的任何其他排列的任何人的处理程序,您可以使用类似的东西来捕获更广泛的案例(因为在内部,您可以只使用/ 进行路由,如果你需要):

app = webapp2.WSGIApplication([
    ('/', MainPage),
    ('/index\..*', MainPage)
])

【讨论】:

  • 谢谢,就是这样!这是我第一个使用 python 而不是 java 的项目。
  • @mike29892 没问题,很乐意提供帮助 - 祝一切顺利。
猜你喜欢
  • 2012-08-19
  • 2015-08-25
  • 1970-01-01
  • 2017-12-09
  • 1970-01-01
  • 2018-05-22
  • 2016-06-22
  • 1970-01-01
  • 2013-03-28
相关资源
最近更新 更多