如何调试 Jinja2 模板?

发布于 2024-08-16 18:31:41 字数 2143 浏览 8 评论 0原文

我在 django 中使用 jinja2 模板系统。 它真的很快,我非常喜欢它。 尽管如此,我在调试模板时遇到了一些问题: 如果我在模板中犯了一些错误(错误的标签、错误的过滤器名称、错误的块结尾......),我根本不知道有关此错误的信息。

例如,在 django 视图中,我这样写:

from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('main', 'templates'))

def jinja(req):
    template = env.get_template('jinja.html')
    output=template.render(myvar='hello')
    return HttpResponse(output)

我编写了一个 jinja2 模板:jinja.html:

{{myvar|notexistingfilter()}} Jinja !

注意,我故意放置了一个不存在的过滤器来生成错误:

我期待类似“notexistingfilter() not Defined”的内容,但我只得到一个简单的黑白回溯(不是通常的 django 调试消息):

Traceback (most recent call last):

  File "/usr/local/lib/python2.6/dist-packages/django/core/servers/basehttp.py", line 279, in run

    self.result = application(self.environ, self.start_response)

  File "/usr/local/lib/python2.6/dist-packages/django/core/servers/basehttp.py", line 651, in __call__
    return self.application(environ, start_response)


  File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/wsgi.py", line 241, in __call__
    response = self.get_response(request)

  File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 134, in get_response

    return self.handle_uncaught_exception(request, resolver, exc_info)

  File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 154, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)


  File "/usr/local/lib/python2.6/dist-packages/django/views/debug.py", line 40, in technical_500_response
    html = reporter.get_traceback_html()

  File "/usr/local/lib/python2.6/dist-packages/django/views/debug.py", line 84, in get_traceback_html

    self.get_template_exception_info()

  File "/usr/local/lib/python2.6/dist-packages/django/views/debug.py", line 117, in get_template_exception_info
    origin, (start, end) = self.exc_value.source



TypeError: 'Template' object is not iterable

我没有得到发生错误的模板文件名,没有有关错误本身的信息,因此调试 jinja2 非常困难。

我应该怎么做才能获得更多调试信息并找到 jinja2 模板中的错误位置?

先感谢您,

I am using jinja2 template system into django.
It is really fast and I like it a lot.
Nevertheless, I have some problem to debug templates :
If I make some errors into a template (bad tag, bad filtername, bad end of block...), I do not have at all information about this error.

For example, In a django view, I write this :

from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('main', 'templates'))

def jinja(req):
    template = env.get_template('jinja.html')
    output=template.render(myvar='hello')
    return HttpResponse(output)

I write a jinja2 template : jinja.html :

{{myvar|notexistingfilter()}} Jinja !

Notice, I put on purpose an non existing filter to generate an error :

I was expecting something like "notexistingfilter() not defined", but I got only a simple black on white traceback (not the usual django debug message) :

Traceback (most recent call last):

  File "/usr/local/lib/python2.6/dist-packages/django/core/servers/basehttp.py", line 279, in run

    self.result = application(self.environ, self.start_response)

  File "/usr/local/lib/python2.6/dist-packages/django/core/servers/basehttp.py", line 651, in __call__
    return self.application(environ, start_response)


  File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/wsgi.py", line 241, in __call__
    response = self.get_response(request)

  File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 134, in get_response

    return self.handle_uncaught_exception(request, resolver, exc_info)

  File "/usr/local/lib/python2.6/dist-packages/django/core/handlers/base.py", line 154, in handle_uncaught_exception
    return debug.technical_500_response(request, *exc_info)


  File "/usr/local/lib/python2.6/dist-packages/django/views/debug.py", line 40, in technical_500_response
    html = reporter.get_traceback_html()

  File "/usr/local/lib/python2.6/dist-packages/django/views/debug.py", line 84, in get_traceback_html

    self.get_template_exception_info()

  File "/usr/local/lib/python2.6/dist-packages/django/views/debug.py", line 117, in get_template_exception_info
    origin, (start, end) = self.exc_value.source



TypeError: 'Template' object is not iterable

I do not get the template filename where the error occured, no information about the error itself, so it is very difficult to debug jinja2.

What should I do to have more debug information and find where an error is inside a jinja2 template ?

Thank you in advance,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

叹沉浮 2024-08-23 18:31:41

做了更多测试后,我找到了答案:

通过直接在 python 下进行相同的模板测试,不使用 django,出现调试消息。所以它来自django。

修复方法在 settings.py 中:必须将 DEBUG 设置为 True 并将 TEMPLATE_DEBUG 设置为 False。

After doing some more test, I found the answer :

By doing the same template test, directly under python, without using django, debug messages are present. So it comes from django.

The fix is in settings.py : One have to set DEBUG to True AND set TEMPLATE_DEBUG to False.

忘羡 2024-08-23 18:31:41

来自 Jinja2 文档:

我的回溯看起来很奇怪。发生什么事了?

如果加速模块未编译,并且您使用的是不带 ctypes 的 Python 安装(不带 ctypes 的 Python 2.4、Jython 或 Google 的 AppEngine),Jinja2 无法提供正确的调试信息,并且回溯可能不完整。目前 Jython 或 AppEngine 没有好的解决方法,因为 ctypes 在那里不可用,并且无法使用加速扩展。

http://jinja.pocoo.org /2/documentation/faq#my-tracebacks-look-weird-what-s-happening

From the Jinja2 Documentation:

My tracebacks look weird. What’s happening?

If the speedups module is not compiled and you are using a Python installation without ctypes (Python 2.4 without ctypes, Jython or Google’s AppEngine) Jinja2 is unable to provide correct debugging information and the traceback may be incomplete. There is currently no good workaround for Jython or the AppEngine as ctypes is unavailable there and it’s not possible to use the speedups extension.

http://jinja.pocoo.org/2/documentation/faq#my-tracebacks-look-weird-what-s-happening

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文