Home > Net >  Jinja2 error - [ 'zip' is undefined] How do I iterate through 2 lists at once in Jinja 2?
Jinja2 error - [ 'zip' is undefined] How do I iterate through 2 lists at once in Jinja 2?

Time:04-16

I have 2 separate arraylist which holds user ids and their title respectively. I want to access the list at once and print them in html. Jinja code:

{% for (i,j) in zip(board.users, board.title) %}
      <div >
      <div >{{ i }}</div>
      <div >{{ j }}</div>
      </div>
{% endfor %}

When I use the zip(foo bar) I get an error - 'zip' is undefined. How is it possible in jinja2?

CodePudding user response:

zip is not part of the Jinja2 global namespace. You can add it, though:

from jinja2 import Environment
env = Environment(
    loader=PackageLoader("yourapp"),
    autoescape=select_autoescape()
)
env.globals.update(zip=zip)

Alternatively, if you only want zip available for a single template, you can extend the environment globals for that template using get_template.

  • Related