gpt4 book ai didi

python - 404 错误页面 jinja2 的 TemplateNotFound

转载 作者:太空宇宙 更新时间:2023-11-03 11:33:43 25 4
gpt4 key购买 nike

我有一个带有 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预先感谢您的帮助

最佳答案

我通过删除 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

关于python - 404 错误页面 jinja2 的 TemplateNotFound,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11425643/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com