如何在GAE中设置动态URL?

时间:2012-06-10 10:08:14

标签: python google-app-engine url

我是GAE的新手。我现在在GAE上主持一个网站。 我想将http://abc.com/about.html的网址更改为http://abc.com/about/ 我应该怎么做?感谢。

这是我的main.py:

import webapp2
from google.appengine.ext.webapp2 import template
from google.appengine.ext.webapp2 import util
import os

class MainHandler(webapp2.RequestHandler):
    def get(self):
        template_values = {}
        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, template_values))

class About(webapp2.RequestHandler):
    def get(self):
        self.response.our.write(template.render('about.html',None))

def main()
    application = webapp2.WSGIApplication([(
        '.', MainHandler),
        ('about', About),
        ])
    util.run_wsgi_app(application)

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

这是我的app.yaml:

application: nienyiho-test
version: 1
runtime: python27
api_version: 1
threadsafe: yes

handlers:
- url: /favicon\.ico
  static_files: favicon.ico
  upload: favicon\.ico

- url: .*
  script: main.app

- url: /about
  static_files: about.html
  upload: about.html

libraries:
- name: webapp2
  version: "2.5.1"

1 个答案:

答案 0 :(得分:2)

您需要更改路线。您没有向我们提供创建路由的代码,但如果您基本上提供静态HTML文件,那么@AdamCrossland注释表明您可以使用app.yaml文件执行此操作。

您的app.yaml文件应如下所示:

application: your_app
version: 1
runtime: python27
api_version: 1
default_expiration: "1d"
threadsafe: True

- url: /about.html
  static_files: static/html/about.html
  upload: static/html/about.html
  secure: never

- url: /about
  script: main.app

- url: /.*
  script: main.app

您也可以使用正则表达式,因为@NickJohnson建议您here如果您愿意,可以删除安全行,但我在某些应用程序中使用https并使用该行强制执行哪些路由是否安全。< / p>

main.py

import webapp2
from google.appengine.ext.webapp2 import template
from google.appengine.ext.webapp2 import util
import os

class MainHandler(webapp2.RequestHandler):
    def get(self):
        template_values = {}
        path = os.path.join(os.path.dirname(__file__), 'index.html')
        self.response.out.write(template.render(path, template_values))

class AboutHandler(webapp2.RequestHandler):
    def get(self):
        self.response.our.write(template.render('about.html',None)

#  Setup the Application & Routes
app = webapp2.WSGIApplication([
    webapp2.Route(r'/', MainHandler),
    webapp2.Route(r'/about', AboutHandler)
], debug=True)

编辑:20120610 - 添加了main.py并更新了app.yaml以显示如何路由生成的内容。