【问题标题】:Why are my Jinja tracebacks meaningless?为什么我的 Jinja 追溯没有意义?
【发布时间】:2012-04-03 18:10:57
【问题描述】:

我不知道为什么,但是最近我从 Jinja 模板渲染中得到的回溯 错误或多或少完全没有意义。

Traceback (most recent call last):
  File "/Users/me/my_project/app/distlib/tipfy/__init__.py", line 442, in wsgi_app
    response = self.handle_exception(request, e)
  File "/Users/me/my_project/app/distlib/tipfy/__init__.py", line 430, in wsgi_app
    rv = self.dispatch(request)
  File "/Users/me/my_project/app/distlib/tipfy/__init__.py", line 559, in dispatch
    return handler(self, request)(method, **request.rule_args)
  File "/Users/me/my_project/app/distlib/tipfy/__init__.py", line 165, in __call__
    response = method(*args, **kwargs)
  File "/Users/me/my_project/app/my_package/admin/menus/editor.py", line 22, in get
    return self.edit_week(menu, year, week_num, week_nums)
  File "/Users/me/my_project/app/my_package/admin/menus/editor.py", line 44, in edit_week
    return self.respond(data, context=ctx, template="admin/menus/edit.html")
  File "/Users/me/my_project/app/my_package/lib/irender.py", line 182, in respond
    return render(self, fmt, opts)
  File "/Users/me/my_project/app/my_package/lib/irender.py", line 207, in __call__
    val = render_func(handler, **opts)
  File "/Users/me/my_project/app/my_package/lib/irender.py", line 220, in render_html
    return handler.render_template(self.template, ctx)
  File "/Users/me/my_project/app/my_package/handlers.py", line 42, in render_template
    return jinja2.get_template(template).render(accum)
  File "/Users/me/my_project/app/distlib/jinja2/environment.py", line 894, in render
    return self.environment.handle_exception(exc_info, True)
  File "/Users/me/my_project/app/templates/admin/menus/edit.html", line 3, in top-level template code
    {% import "admin/utils.html" as utils %}
TypeError: integer argument expected, got float

当然,我可以通过删除大部分 模板并查看错误何时开始发生,但这是一个愚蠢的开发 方法。

如何使回溯再次有用?

顺便说一句,我的 main.py 中有以下咒语:

def whitelist_jinja2_debug_modules():
    from google.appengine.tools.dev_appserver import HardenedModulesHook
    HardenedModulesHook._WHITE_LIST_C_MODULES += ['_ctypes', 'gestalt']

if app.dev:
    whitelist_jinja2_debug_modules()

【问题讨论】:

  • 原来问题出在通过{% import ... %} 导入的模板中,错误的行是{% for base in range(52 / 13) %} - 但问题仍然存在。
  • 把你的计算放在 python 代码而不是模板中?
  • 似乎是一种短视的用户界面设计方法。

标签: google-app-engine jinja2


【解决方案1】:

尽管这个问题已经很老了,但答案很容易获得(而不是同时)。要在 Jinja 中获得正确的堆栈跟踪,您需要拥有在 Google 应用引擎中很长时间不可用的 ctypes。我不完全确定当前的状态,但这个问题为您提供了一些解决方案: Debug Jinja2 in Google App Engine

【讨论】:

    猜你喜欢
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    • 2021-10-16
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多