html/htmページ、写真などだけの単純な静的WebサイトにGAEを使用しています。Python 2.7も使用しています。
だから私は簡単な 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 を使用した単純な Web サイトでこれを行う方法を正確に説明しているものはありません。
教えてください、どうもありがとう、マイケル