1

我正在使用GAE简单的静态网站只有HTML/HTM页面,图片等。我也使用Python 2.7。GAE中的简单静态网站与自定义404错误页面

所以我使用了一个直接的app.yaml和main.py,并且工作。但是,当访问一个不存在的页面时,它会显示一个标准的404页面。我想将其更改为自定义错误页面,并在下面尝试过,但不起作用。

这里有我的app.yaml和main.py文件:

application: xxxx 
version: 11 
runtime: python27 
api_version: 1 
threadsafe: true 

default_expiration: "7d" 

inbound_services: 
- warmup 

handlers: 
- url:/
    static_files: index.html 
    upload: index.html 

- url: /(.*) 
    static_files: \1 
    upload: (.*) 

- url: /.* 
    script: main.app 

Main.py:

import webapp2 

class BaseHandler(webapp2.RequestHandler): 
    def handle_exception(self, exception, debug): 
    # Set a custom message. 
    self.response.write('An error occurred.') 

    # If the exception is a HTTPException, use its error code. 
    # Otherwise use a generic 500 error code. 
    if isinstance(exception, webapp2.HTTPException): 
     self.response.set_status(exception.code) 
    else: 
     self.response.set_status(500) 

class MissingPage(BaseHandler): 
    def get(self): 
    self.response.set_status(404) 
    self.response.write('Page has moved. Pls look at http://www.yyyyyy.yy to find the new location.') 

class IndexHandler(webapp2.RequestHandler): 
    def get(self): 
     if self.request.url.endswith('/'): 
      path = '%sindex.html'%self.request.url 
     else: 
      path = '%s/index.html'%self.request.url 

     self.redirect(path) 

    def post(self): 
     self.get() 

app = webapp2.WSGIApplication(
    [ (r'/', IndexHandler), 
     (r'/.*', MissingPage) 
     ], 
    debug=True) 

什么是不正确的?我找了很多项目,但没有确切地解释了如何为一个简单的网站与Python 2.7为此,

让我知道,许多感谢,迈克尔

+0

省略了.yaml文件中的第二个处理程序,你会看到你的main.app你处理的结果。第二个处理程序应该做什么 - 'static_files':\ 1? – rGil 2013-05-13 19:26:27

+0

谢谢RGIL,这是一个很好的线索,我试图拿出第二个处理程序,我没有得到404自定义错误页面。但现在它只有index.html页面,而不是其他(105)页面,图像,样式表等。 我使用第二个处理程序来处理所有其他页面。 所以有办法,我仍然可以提供所有105个文件作为静态文件并获得自定义错误页面。 – user2378683 2013-05-14 03:24:57

回答

2

看起来它并不真的需要有您的网站的任何动态部分,除了404页面。 有一个error_handlers可以直接使用。

https://developers.google.com/appengine/docs/python/config/appconfig#Custom_Error_Responses

application: xxxx 
version: 11 
runtime: python27 
api_version: 1 
threadsafe: true 

default_expiration: "7d" 

inbound_services: 
- warmup 

handlers: 
- url:/
    static_files: index.html 
    upload: index.html 

- url: /(.*) 
    static_files: \1 
    upload: (.*) 

error_handlers: 
- file: default_error.html