Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to debug Jinja2 template?

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,

like image 479
Eric Avatar asked Jan 04 '10 21:01

Eric


People also ask

What is the difference between Jinja and Jinja2?

Jinja, also commonly referred to as "Jinja2" to specify the newest release version, is a Python template engine used to create HTML, XML or other markup formats that are returned to the user via an HTTP response.

What is Jinja2 template in Ansible?

Jinja2 templates are simple template files that store variables that can change from time to time. When Playbooks are executed, these variables get replaced by actual values defined in Ansible Playbooks. This way, templating offers an efficient and flexible solution to create or alter configuration file with ease.

Does Jinja2 work with Python 3?

Jinja2 works with Python 2.6. x, 2.7. x and >= 3.3. If you are using Python 3.2 you can use an older release of Jinja2 (2.6) as support for Python 3.2 was dropped in Jinja2 version 2.7.


1 Answers

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.

like image 56
Eric Avatar answered Sep 30 '22 13:09

Eric