【问题标题】:MainHandler doe not handle all page requests - Google App EngineMainHandler 不处理所有页面请求 - Google App Engine
【发布时间】:2013-11-29 04:07:59
【问题描述】:

我是 Google App Engine 的新手,我正在尝试创建一个简单的多页应用程序(index.htm、sites.htm、topics.htm)。当我运行没有文件名的应用程序时,一切都很好http://localhost:9080。但是当我尝试加载特定页面 http://localhost:9080/index.htmhttp://localhost:9080/sites.htmhttp://localhost:9080/topics.htm 时,我收到 404 错误

这是我的 app.yaml

application: msa
version: 1
runtime: python27
api_version: 1
threadsafe: yes

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

- url: /static
  static_dir: static

- url: .*
  script: main.app

libraries:
- name: webapp2
  version: "2.5.2"

我的 MainHandler 如下

class MainHandler(webapp2.RequestHandler):
    def get(self):      
        path = self.request.path
        temp = os.path.join(os.path.dirname(__file__),'templates/' + path)
        self.response.write(temp + '<br />')
        if not os.path.isfile(temp):
            temp = os.path.join(os.path.dirname(__file__),'templates/index.htm')

        outstr = template.render(temp, { })
        self.response.out.write(outstr)

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

我所有的文件都是以这种方式组织的

/
/app.yaml
/index.yaml
/main.py
/static
/static/glike.css
/templates
/templates/_base.htm
/templates/index.htm
/templates/topics.htm
/templates/sites.htm

任何指导将不胜感激

【问题讨论】:

    标签: google-app-engine python-2.7 webapp2 multipage


    【解决方案1】:

    你必须创建路由:http://webapp-improved.appspot.com/guide/routing.html#simple-routes

    ('/', MainHandler) 只会处理:/

    要处理所有请求,请使用:

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

    【讨论】:

    • 我可以创建通用路由以在单个处理程序中处理 *.htm 之类的事情吗?
    猜你喜欢
    • 1970-01-01
    • 2018-05-18
    • 2012-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多