我已經試過一切,但似乎你不能得到一個包羅萬象的網址...捕獲所有腳本在App Engine中的Python(app.yaml中)沒有在靜態文件工作
- url: /.*
script: not_found.py
...到在基於靜態目錄路徑的網址上工作。例如。我可以輸入www.foobar.com/asdas/asd/asd/asd/ad/sa/das/d,我可以獲得一個很好的自定義404頁面。但是,如果我改變靜態路徑URL像www.foobar.com/mydir/mydir/mypage.html,我只是得到了可怕的通用404 ....
Error: Not Found
The requested URL /mydir/mydir/mypage.html was not found on this server.
...我想改變什麼捕獲目錄路徑中的url並寫入404。這似乎是在GAE Python中獲得一致的自定義404頁面的唯一方法。
任何人都可以幫忙嗎?我從頭開始編寫我的網站,對Python的知識非常有限。實現一致的自定義404是我無法克服的唯一事情。
編輯/添加:好的我已經添加了@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()
NotFound類的前兩行應該在導入之下..如文檔https://developers.google。com/appengine/docs/python/gettingstartedpython27/templates – Lipis
對不起,而不是post(self)> get(self)..我正在更新我的答案.. – Lipis
使MainHandler在示例波紋管中的位置..並更改'應用程序=',以我寫在那裏,並確保你可以看到你的index.html頁面,當你在主頁上,其他任何事情都會返回404.html ..如果這樣的話你可以用第一步..稍後你將不得不瞭解URL映射是如何工作的以及這些東西是什麼:'/.*','/(.* html)?'。他們是正規表達式,最有可能你必須閱讀他們以及:)但當時的一步.. – Lipis