python – 404错误页面jinja2的TemplateNotFound
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – 404错误页面jinja2的TemplateNotFound,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4464字,纯文字阅读大概需要7分钟。
内容图文
![python – 404错误页面jinja2的TemplateNotFound](/upload/InfoBanner/zyjiaocheng/777/594b385839244c4e9c4d6cce320482c2.jpg)
我有一个带有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
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',
}
这是文件夹结构
另一个重要的事情是它在本地机器(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全部内容,希望文章能够帮你解决python – 404错误页面jinja2的TemplateNotFound所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。