下面是我的 app.yaml
文件的代码。如果我转到 localhost:8080/
,我的 index.app
会正确加载。如果我转到 localhost:8080/index.html
,我会收到 404 错误。如果我转到任何其他页面,例如 localhost:8080/xxxx
,not_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.html
或 index.
的任何其他排列的处理程序,您可以使用类似这样的东西来捕获更广泛的案例(因为在内部,如果需要,您可以使用 /
进行路由):
app = webapp2.WSGIApplication([
('/', MainPage),
('/index\..*', MainPage)
])
关于Python 2.7 GAE app.yaml 获取 404 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13555534/