0

My GAE project has a setup like:

/static/.* --> images, css, js
/rest/.* --> a script handling all rest resources using webapp2 handlers

I want to use jinja2 templates to basically create some html pages but e.g. using the template inheritance jinja2 provides. More or less to do server side includes.

So all other incoming requests should directly render a template, e.g:

/ --> index template
/index.html --> index template
/some/path/to/a/page.html --> /some/path/to/a/page template
/some/path/to/a/page --> /some/path/to/a/page template

I would like to match both .html and paths without extension.

I don't want to create routes for all my paths, just some smart script that can handle this. Would this be possible?

4

1 回答 1

4

是的你可以。但仍然需要单个处理程序:

# app.yaml
- url: /rest/.*
  script: main.app

# main.py
class PageHandler(webapp2.RequestHandler):
    def get(self, page):
        if not page.endswith('.html'):
            page += '.html'
        self.response.write(self.jinja2.render_template(page))

app = webapp2.WSGIApplication([
    webapp2.RedirectRoute('/rest/<page>', PageHandler, name='page'),
], debug=True)

然后你只需将你的页面链接到 /rest/index.html 或 /rest/path/to/page

但是,如果您纯粹将它用于静态文件,它仍将使用实例来生成这些页面,如果您愿意,可以使用我的 app-engine-static github 项目。它基本上是一个项目,可以帮助您使用 jinja2 构建动态站点,然后生成静态文件,这将与应用引擎的内置 cdn 一起使用,并且不会消耗实例时间: http ://blog.altlimit.com/2013/08/host -static-website-on-google-app.html

于 2013-08-30T15:16:21.140 回答