2012-07-11 78 views
4

我有Jinja2的谷歌的App Engine應用程序,當我強迫404錯誤我有這樣的錯誤:TemplateNotFound的404錯誤頁面的Jinja2

errors/default_error.html 
Traceback (most recent call last): 
    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1536, in __call__ 
rv = self.handle_exception(request, response, e) 
    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1596, in handle_exception 
return handler(request, response, e) 
    File "/base/data/home/apps/s~sandengine/latest.360189283466406656/main.py", line 28, in handle_404 
t = jinja2.get_jinja2(app=app).render_template(template, **c) 
    File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2_extras/jinja2.py", line 158, in render_template 
return self.environment.get_template(_filename).render(**context) 
    File "/base/python27_runtime/python27_lib/versions/third_party/jinja2-2.6/jinja2/environment.py", line 719, in get_template 
return self._load_template(name, self.make_globals(globals)) 
    File "/base/python27_runtime/python27_lib/versions/third_party/jinja2-2.6/jinja2/environment.py", line 693, in _load_template 
template = self.loader.load(self, name, globals) 
    File "/base/python27_runtime/python27_lib/versions/third_party/jinja2-2.6/jinja2/loaders.py", line 115, in load 
source, filename, uptodate = self.get_source(environment, name) 
    File "/base/python27_runtime/python27_lib/versions/third_party/jinja2-2.6/jinja2/loaders.py", line 180, in get_source 
raise TemplateNotFound(template) 
TemplateNotFound: errors/default_error.html 

這裏我yaml file:

application: sandengine 
version: latest 
runtime: python27 
api_version: 1 
threadsafe: true 

default_expiration: "30d" 

skip_files: 
- ^(.*/)?app\.yaml 
- ^(.*/)?app\.yml 
- ^(.*/)?index\.yaml 
- ^(.*/)?index\.yml 
- ^(.*/)?#.*# 
- ^(.*/)?.*~ 
- ^(.*/)?.*\.py[co] 
- ^(.*/)?.*/RCS/.* 
- ^(.*/)?\..* 
- ^(.*/)?tests$ 
- ^(.*/)?test$ 
- ^Makefile 
- ^COPYING.LESSER 
- ^README.rdoc 
- \.gitignore 
- ^\.git/.* 
- \.*\.lint$ 

builtins: 
- appstats: on #/_ah/stats/ 
- remote_api: on #/_ah/remote_api/ 

handlers: 
- url: /favicon\.ico 
    mime_type: image/vnd.microsoft.icon 
    static_files: static/favicon.ico 
    upload: static/favicon.ico 

- url: /apple-touch-icon\.png 
    static_files: static/apple-touch-icon.png 
    upload: static/apple-touch-icon.png 

- url: /(robots\.txt|humans\.txt|crossdomain\.xml) 
    static_files: static/\1 
    upload: static/(robots\.txt|humans\.txt|crossdomain\.xml) 

- url: /img/(.*\.(gif|png|jpg)) 
    static_files: static/img/\1 
    upload: static/img/(.*\.(gif|png|jpg)) 

- url: /css 
    mime_type: text/css 
    static_dir: static/css 

- url: /js 
    mime_type: text/javascript 
    static_dir: static/js 

- url: /.* 
    script: main.app 

libraries: 
- name: jinja2 
    version: "2.6" 
- name: webapp2 
    version: "2.5.1" 
- name: markupsafe 
    version: "0.15" 

error_handlers: 
    - file: templates/errors/default_error.html 

    - error_code: over_quota 
    file: templates/errors/over_quota.html 

    - error_code: dos_api_denial 
    file: templates/errors/dos_api_denial.html 

    - error_code: timeout 
    file: templates/errors/timeout.html 

代碼:

def handle_404(request, response, exception): 
    c = { 'exception': exception.status } 
    template = config.error_templates[404] 

    t = jinja2.get_jinja2(app=app).render_template(template, **c) 
    response.write(t) 
    response.set_status(exception.status_int) 

app = webapp2.WSGIApplication(debug = os.environ['SERVER_SOFTWARE'].startswith('Dev'), config=config.webapp2_config) 

app.error_handlers[404] = handle_404 
routes.add_routes(app) 

配置文件:

error_templates = { 
    404: 'errors/default_error.html', 
    500: 'errors/default_error.html', 
} 

這裏是文件夾結構

enter image description here

另一個重要的事情是,它沒有在本地機器(SDK)的問題,但問題出現在生產

您可以探索完整代碼,因爲這是一個開放的source code 在此先感謝您的幫助

+2

如果複製'會發生什麼錯誤'目錄到項目'root'目錄? – 2012-07-11 19:04:56

+0

同樣的錯誤! – coto 2012-07-14 17:05:46

回答

2

我通過刪除「default_error」 pp.yaml

和錯誤處理程序,現在只需要:

error_handlers: 
    - error_code: over_quota 
    file: templates/errors/over_quota.html 

    - error_code: dos_api_denial 
    file: templates/errors/dos_api_denial.html 

    - error_code: timeout 
    file: templates/errors/timeout.html 

我改進了控制404錯誤代碼,添加500錯誤,有:

def handle_error(request, response, exception): 
    c = { 'exception': str(exception) } 
    status_int = hasattr(exception, 'status_int') and exception.status_int or 500 
    template = config.error_templates[status_int] 
    t = jinja2.get_jinja2(app=app).render_template(template, **c) 
    response.write(t) 
    response.set_status(status_int) 

app = webapp2.WSGIApplication(debug = os.environ['SERVER_SOFTWARE'].startswith('Dev'), config=config.webapp2_config) 

app.error_handlers[404] = handle_error 
app.error_handlers[500] = handle_error