2

每当我的 App Engine 项目中继承的 Jinja2 模板出现错误时,我都会得到一个回溯,它仅指定导致继承的行,而不是导致错误的行。

例如,我创建了一个带有两个模板的虚拟项目,page.html并且base.html. page.html继承自base.htmlbase.html具有设计为导致错误的行。App Engine/Jinja 不会报告错误,而是base.html给我这个回溯:

Traceback (most recent call last):
  File "/home/kkinder/Software/google_appengine/lib/webapp2/webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "/home/kkinder/Software/google_appengine/lib/webapp2/webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "/home/kkinder/Software/google_appengine/lib/webapp2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/home/kkinder/Software/google_appengine/lib/webapp2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/home/kkinder/Software/google_appengine/lib/webapp2/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/home/kkinder/Software/google_appengine/lib/webapp2/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/home/kkinder/Projects/JinjaTestProject/main.py", line 11, in get
    self.response.out.write(self.jinja2.render_template('page.html'))
  File "/home/kkinder/Software/google_appengine/lib/webapp2/webapp2_extras/jinja2.py", line 158, in render_template
    return self.environment.get_template(_filename).render(**context)
  File "/home/kkinder/Software/google_appengine/lib/jinja2/jinja2/environment.py", line 894, in render
    return self.environment.handle_exception(exc_info, True)
  File "templates/page.html", line 1, in top-level template code
    {% extends 'base.html' %}
UndefinedError: 'doesnotexist' is undefined

主要.py:

import webapp2
from webapp2_extras import jinja2

class MainHandler(webapp2.RequestHandler):
    @webapp2.cached_property
    def jinja2(self):
        return jinja2.get_jinja2(app=self.app)

    def get(self):
        self.response.out.write(self.jinja2.render_template('page.html'))

app = webapp2.WSGIApplication([('/', MainHandler)],
                             debug=True)

page.html:

{% extends 'base.html' %}
{% block content %}
    Stuff
{% endblock %}

base.html:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
</head>
<body>
<h1>{{ doesnotexist.foo }}</h1>
    {% block content %}{% endblock %}
</body>
</html>
4

1 回答 1

1

不幸的是,在生产环境中无法在 GAE 上获得更好的 Jinja2 调试信息,因为它们限制了对所需软件包的访问。您可以在官方常见问题解答中找到有关此问题的更多信息:我的回溯看起来很奇怪。发生了什么?

于 2012-07-09T02:49:17.070 回答