Google App Engine and 404 error

前端 未结 9 1251
一生所求
一生所求 2020-12-02 06:46

I\'ve setup a static website on GAE using hints found elsewhere, but can\'t figure out how to return a 404 error. My app.yaml file looks like

- url: (.*)/
           


        
9条回答
  •  醉酒成梦
    2020-12-02 07:17

    You need to register a catch-all script handler. Append this at the end of your app.yaml:

    - url: /.*
      script: main.py
    

    In main.py you will need to put this code:

    from google.appengine.ext import webapp
    from google.appengine.ext.webapp.util import run_wsgi_app
    
    class NotFoundPageHandler(webapp.RequestHandler):
        def get(self):
            self.error(404)
            self.response.out.write('')
    
    application = webapp.WSGIApplication([('/.*', NotFoundPageHandler)],
                                         debug=True)
    
    def main():
        run_wsgi_app(application)
    
    if __name__ == "__main__":
        main()
    

    Replace with something meaningful. Or better use a template, you can read how to do that here.

    Please let me know if you have problems setting this up.

提交回复
热议问题