List comprehensions in Jinja

Since v2.7 there is selectattr: Filters a sequence of objects by applying a test to the specified attribute of each object, and only selecting the objects with the test succeeding. If no test is specified, the attribute’s value will be evaluated as a boolean. Example usage: {{ users|selectattr(“is_active”) }} {{ users|selectattr(“email”, “none”) }} Similar to … Read more

Replace multiple values with jinja2

Your expression seems to work just fine. If I create a template with the jinja2 expression from your question: >>> import jinja2 >>> t = jinja2.Template(‘{{ test1 | replace(“value1″,”my_value1”) | replace(“value2″,”my_value2”) }}’) And then render it, passing in a value for test1 that contains both of the target replacement strings: >>> output = t.render(test1=”this has … Read more

How to get a list of current variables from Jinja 2 template?

Technically, because context is not passed as a named dictionary, a little work is required to generate a list of the context variables from inside a template. It is possible though. Define a Jinja context function to return the jinja2.Context object, which is essentially a dictionary of the global variables/functions Make that function available in … Read more

How to debug Jinja2 template?

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.