1

私はすべてを試しましたが、キャッチオール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 ()
4

2 に答える 2

4

理解を深めるために、Getting Startedの例にいくつかの変更を加えます。これは、既に実行済みであり、それを使用していくつかの実験を行ったことを前提としています。

app.yamlより動的なものを表示したい可能性が高く、通常は- url: /.*アプリ内で処理する必要があるため、見つからないすべてのページに静的ファイルを用意することはお勧めできません。

この例では、見つからないRequestHandlerすべてのページに新しい

import jinja2
import os
# more imports

jinja_environment = jinja2.Environment(loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

class MainPage(webapp2.RequestHandler):
    def get(self):
         template = jinja_environment.get_template('index.html')
         self.response.out.write(template.render(template_values))

class NotFound(webapp.RequestHandler):
    def get(self):
         # you need to create the not_found.html file
         # check Using Templates from Getting Started for more
         template = jinja_environment.get_template('not_found.html')
         self.response.out.write(template.render(template_values))

application = webapp.WSGIApplication(
                                     [('/', MainPage),
                                      ('/.*', NotFound)], # <-- This line is important
                                      debug=True)

ただし、jinja2 テンプレートを機能させるには、「はじめに」の「テンプレートの使用」セクションで行う必要がある変更に注意深く従ってください。

URL マッピングの順序は非常に重要であるため、このキャッチすべての正規表現 ( /.*) は常に最後のものにする必要があります。そうしないと、他のすべてのルールがスキップされるためです。

于 2012-09-08T11:26:52.450 に答える
0

すべての URL をキャッチしたい場合は、「/.*」を追加して、ファイル「not_found.py」のメイン リクエスト ハンドラを変更する必要があります。

たとえば、ファイル「not_found.py」を次のように設定できます。

from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class MainHandler(webapp.RequestHandler):
  def get(self):
    self.response.out.write("Hello, MAIN!")

application = webapp.WSGIApplication(
                                 [('/.*', MainHandler)], # <--- Add '/.*' here
                                 debug=True)

def main():
    run_wsgi_app(application)

www.foobar.com/asd/ad/sa/das/d またはその他の URL に移動すると、「Hello, MAIN!.

それが役に立てば幸い。必要に応じて質問する

于 2012-09-08T08:33:09.613 に答える