如何使用App Engine获取Jinja2中继承模板的特定回溯?

2024-04-26 03:47:45 发布

您现在位置:Python中文网/ 问答频道 /正文

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

例如,我用两个模板创建了一个虚拟项目,page.htmlbase.htmlpage.html继承自base.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公司名称:

^{pr2}$

在页面.html公司名称:

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

在基本.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>

Tags: inpyselfhomebasereturnlibhtml
1条回答
网友
1楼 · 发布于 2024-04-26 03:47:45

不幸的是,在生产中不可能获得关于GAE的Jinja2更好的调试信息,因为他们限制了对所需包的访问。您可以在官方的FAQ: My tracebacks look weird. What’s happening?上找到有关此问题的更多信息

相关问题 更多 >