简体   繁体   English

如何调试Jinja2模板?

[英]How to debug Jinja2 template?

I am using jinja2 template system into django. 我正在使用jinja2模板系统进入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. 不过,我调试模板时遇到了一些问题:如果我在模板中犯了一些错误(错误的标签,坏的filtername,坏的结尾......),我根本没有关于这个错误的信息。

For example, In a django view, I write this : 例如,在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)

I write a jinja2 template : jinja.html : 我写了一个jinja2模板: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) : 我期待像“notexistingfilter()未定义”这样的东西,但我只有一个简单的黑色白色追溯(不是通常的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

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. 我没有得到错误发生的模板文件名,没有关于错误本身的信息,因此调试jinja2非常困难。

What should I do to have more debug information and find where an error is inside a jinja2 template ? 我该怎么做才能获得更多调试信息并找到jinja2模板中的错误?

Thank you in advance, 先感谢您,

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. 通过直接在python下进行相同的模板测试,不使用django,就会出现调试消息。 So it comes from django. 所以它来自django。

The fix is in settings.py : One have to set DEBUG to True AND set TEMPLATE_DEBUG to False. 修复程序位于settings.py中:必须将DEBUG设置为True并将TEMPLATE_DEBUG设置为False。

From the Jinja2 Documentation: 来自Jinja2文档:

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. 如果未编译speedups模块并且您使用的是没有ctypes的Python安装(没有ctypes的Python 2.4,Jython或Google的AppEngine),Jinja2无法提供正确的调试信息,并且回溯可能不完整。 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. 目前没有针对Jython或AppEngine的良好解决方法,因为ctypes在那里不可用,并且不可能使用speedups扩展。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM