Reputation: 3003
I am new to both Python and Jinja2. I'd like to read a value in a dictionary of a list. I think there is an answer for such operation here. Unfortunately this does not seem to work in Jinja2. I get this:
jinja2.exceptions.TemplateSyntaxError: Encountered unknown tag 'item'.
From what I know Jinja2 does not comprehend full Python, which I think is at the heart of the problem here. Can anyone please confirm?
Upvotes: 0
Views: 81
Reputation: 172
Example using Flask:
main.py
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def hello_world():
dicts = [
{ "name": "Tom", "age": 10 },
{ "name": "Mark", "age": 5 },
{ "name": "Pam", "age": 7 },
{ "name": "Dick", "age": 12 }
]
return render_template("test.html", dicts = dicts)
if __name__ == '__main__':
app.run(debug = True)
In folder templates
test.html
<html>
<head>
<table>
<tr>
<th>Name</th>
<th>Age</th>
</tr>
{% for dic in dicts %}
{%if dic['name'] == 'Pam'%}
<tr><td><b>{{dic['name']}}</b></td><td><b>{{dic['age']}}</b></td></tr>
{%else%}
<tr><td>{{dic['name']}}</td><td>{{dic['age']}}</td></tr>
{%endif%}
{% endfor %}
</table>
</body>
</html>
Output:
Upvotes: 1