0

I am trying to run this code in my HTML:

<tbody>
      {% for name, npcBuy in zip(farmingMerchantName, farmingMerchantPrices) %}
      <tr>
        <td>{{ name }}</td>
        <td>temp</td>
        <td>{{ npcBuy }}</td>
        <td>temp</td>
      </tr>
      {% endfor%}
    </tbody>

But I get the error: jinja2.exceptions.UndefinedError: 'zip' is undefined

I have tried these following things in Python as well:

import jinja2

app.jinja_env.globals.update(zip=zip)

And this:

Import jinja2
env = jinja2.Environment()
env.globals.update(zip=zip)

(not at the same time)

from the first one, I get the error "Method 'jinja_env' has no 'globals' member.

And from the second one, well I get the same error (jinja2.exceptions.UndefinedError: 'zip' is undefined)

I have checked this thread out, but it did not help.

Thanks

  • You are not defining zip before trying to pass it as a keyword arg to the function. What you expecting to happen here? – jordanm May 26 '20 at 18:46
  • 2
    @jordanm `zip` is a built-in function in Python. – Thomas May 26 '20 at 18:47
  • What do you mean? @jordanm –  May 26 '20 at 18:47
  • This does not answer the question, but have you thought about using zip from your .py file before passing it to your .html file? – Akib Rhast May 26 '20 at 18:47
  • Well, I am trying to display items from an array in a table, I tried to do two "for loops" inside of my HTML and that didn't work, then came across "zip", but cant seem to get that to work –  May 26 '20 at 18:49
  • @SimonSjöö Have you tried creating said array on your routes, instead of trying to create it in the html page? Could you please post what the route for this particular page looks like currently? – Akib Rhast May 26 '20 at 18:56
  • This is my pyhon code for that route: https://pastebin.com/cykfjyvY –  May 26 '20 at 19:16
  • I got it to work! Still, it says the problem "Method 'jinja_env' has no 'globals' member", which is a bit strange.... I restarted VSC and it started to work though :) –  May 26 '20 at 19:19

2 Answers2

3

zip is a Python function as such you will need to pass the same way you pass other variables to be used in your jinja template.


    # flask code 
    return render_template('page.html',
                           column_names = df.columns.values,
                           row_data=list(df.values.tolist()),
                           zip=zip)
0

you need zip() but it isn't defined in jinja2 templates.

one solution is zipping it before render_template function is called, like:

view function:

return render(request, "template_name", context={"data":zip(farmingMerchantName, farmingMerchantPrices)})

template:

<tbody>
  {% for farmingMerchantName, farmingMerchantPrices in data %}
  <tr>
    <td>{{ farmingMerchantName }}</td>
    <td>temp</td>
    <td>{{ farmingMerchantPrices  }}</td>
    <td>temp</td>
  </tr>
  {% endfor%}
</tbody>

also, you can add zip to jinja2 template global, using Flask.add_template_x functions(or Flask.template_x decorators)

@app.template_global(name='zip')
def _zip(*args, **kwargs): #to not overwrite builtin zip in globals
    return __builtins__.zip(*args, **kwargs)
BiswajitPaloi
  • 586
  • 4
  • 16