Python 2.7 GAE app.yaml 获取 404 错误

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

下面是我的 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):

template = jinja_environment.get_template('index.html')

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

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

修复位于粗体文本中!

最佳答案

看起来 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 2.7 GAE app.yaml 获取 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13555534/

相关文章:

python - 通过维护列表中值的映射顺序来随机化两个列表

python - 防止文本框在更新时返回到文本框的开头

google-app-engine - Google App Engine - 从 Python 代码将文件上传到 blobstore 时出错

java - 在 Google 应用引擎上部署失败

google-app-engine - AppEngine Java 全文。搜索子字符串(SDK 1.6.6)

python - 带有 --always-copy 的 virtualenv 抛出错误 "Operation not permitted"

python - 在 python pandas 中获取剩余聚合列作为两列分组的列表

python - pygame 函数似乎被忽略

python - Django 抛出 ValueError : too many values to unpack (expected 2) with no change to the code

python - 加快 Pandas 中日期计算之间的时间?