1

我已經試過一切,但似乎你不能得到一個包羅萬象的網址...捕獲所有腳本在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() 
+0

NotFound類的前兩行應該在導入之下..如文檔https://developers.google。com/appengine/docs/python/gettingstartedpython27/templates – Lipis

+0

對不起,而不是post(self)> get(self)..我正在更新我的答案.. – Lipis

+0

使MainHandler在示例波紋管中的位置..並更改'應用程序=',以我寫在那裏,並確保你可以看到你的index.html頁面,當你在主頁上,其他任何事情都會返回404.html ..如果這樣的話你可以用第一步..稍後你將不得不瞭解URL映射是如何工作的以及這些東西是什麼:'/.*','/(.* html)?'。他們是正規表達式,最有可能你必須閱讀他們以及:)但當時的一步.. – Lipis

回答

4

爲了更好的理解我會在Getting Started例如一些修改,我假設你已經做了你用它做了一些實驗

app.yaml中的所有未找到的頁面都有靜態文件不是一個好主意,因爲很可能您希望顯示更具動態性的內容,並且通常應該在您的應用中處理- url: /.*

在這個example我們將增加一個新的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的模板工作頁面,請細心一點,你需要做的修改Using Templates section入門指南。

URL映射中的順序非常重要,因此捕獲所有正則表達式(/.*)應始終是最後一個,因爲否則所有其他規則都將被跳過。

+0

非常感謝你。我正在通過這個工作,我會讓你知道我是如何得到的:D – H4NIO

+0

@Hannah確保你仔細閱讀了入門教程,它可以在你的計算機上運行,​​然後簡單地添加這個NotFound處理程序和URL映射行:) – Lipis

+0

@ Lipis我已經添加了更多的我的原始Q :) – H4NIO

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或任何其他網址,你會看到消息「你好,主!

希望它能幫助。問的問題,如果需要

+0

接受答案,如果它是有用的;) – Littm

+0

這個評論是沒有用的..閱讀一些答案和評論從這個問題.. http://meta.stackexchange.com/a/146027/142717;) – Lipis

+0

是的你是對的。我會在下次嘗試給出更多解釋:S。感謝您的鏈接。 – Littm

相關問題