私はすべてを試しましたが、キャッチオールURLを取得できないようです...
- url: /.*
script: not_found.py
...静的ディレクトリ パスに基づく URL で動作します。例えば。www.foobar.com/asdas/asd/asd/asd/ad/sa/das/d と入力すると、素敵なカスタム 404 ページを取得できます。しかし、www.foobar.com/mydir/mydir/mypage.html のような静的パスの URL を変更すると、恐ろしい一般的な 404 が表示されます....
Error: Not Found
The requested URL /mydir/mydir/mypage.html was not found on this server.
... ディレクトリ パスの URL をキャッチして 404 を書き込むものを変更したいと思います。これは、GAE Python で一貫したカスタム 404 ページを取得する唯一の方法のようです。
誰でも助けることができますか?私はウェブサイトをゼロから作成しましたが、Python の知識は非常に限られています。一貫したカスタム 404 を達成することは、私が克服できないと思われる唯一のものです。
EDIT/ADD : OK @Lipis の親切な提案を追加し、ありがたいことにクラスの理解を深めることができました(残念ながらまだ投票できません)。しかし!ネットで見つかった .py スクリプトを使用していますが、NotFound クラスがインデックス ページを提供するクラスに干渉していると思います。これは、インデックス ページが Jinja によって指定された 404 ページであるためです。私は MainHandler についてほとんど理解していないので、今のところあきらめなければならないかもしれません。
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
from google.appengine.ext.webapp.util import run_wsgi_app
import jinja2
class MainHandler(webapp.RequestHandler):
def get (self, q):
if q is None:
q = 'index.html'
path = os.path.join (os.path.dirname (__file__), q)
self.response.headers ['Content-Type'] = 'text/html'
self.response.out.write (template.render (path, {}))
class NotFound(webapp.RequestHandler):
def post(self):
# you need to create the not_found.html file
# check Using Templates from Getting Started for more
jinja_environment = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))
template = jinja_environment.get_template('404.html')
self.response.out.write(template.render(template_values))
def main ():
application = webapp.WSGIApplication ([('/(.*html)?', MainHandler),('/.*', NotFound)],
debug=True)
util.run_wsgi_app (application)
if __name__ == '__main__':
main ()