sarsa
sarsa

Reputation: 37

python flask app, using dictionary with variables

So I'm trying to reach dictionary parameters with a variable but looks like you can't use it with the traditional way (mydict[var])

I'm passing a dictionary and a list to HTML:

@app.route('/')
def setup():
   jinda = {
     'first':'1234'
     'second':'4567'
     'third': '7890'
    }
   zirna = ['first', 'third']

   return render_template('table.html', zirna=zirna, jinda=jinda)

Now in HTML I want to use a for loop two reach specific elements in the dictionary which is also in the list but I don't know how to do that also this one is not working 👇:

{% for i in zirna %}

<p> {{ jinda[i] }}<p>

{% endfor %}

Upvotes: 0

Views: 275

Answers (1)

v25
v25

Reputation: 7631

This works for me and outputs:

<p> 1234<p>
 
<p> 7890<p>

However there is a syntax error in your python code. Commas should be used between each key:value when defining the dictionary:

   jinda = {
     'first':'1234',
     'second':'4567',
     'third': '7890',
    }

Perhaps this is your issue, although I would expect this to throw a syntax error when launching your app.

Upvotes: 1

Related Questions