TemplateNotFound为404错误页面jinja2

时间:2012-07-11 04:38:22

标签: python google-app-engine jinja2

我有一个带有jinja2的Google App Engine应用,当我强制404错误时,我有这个错误:

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 在此先感谢您的帮助

1 个答案:

答案 0 :(得分:2)

我通过删除app.yaml上的“default_error”来修复它

错误处理程序现在只有:

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