I'm getting a weird problem in jinja. It seems simple but i'm getting it right. In a jinja template with {{tag["tag"] }}
it is echoing {u'type': u'literal', u'value': u'tourism'}
but when I am trying to get the value with {{tag["tag"]["value"] }}
, I am getting the error jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'tag'
from the following strace:
Traceback (most recent call last):
File "vocabularies.py", line 16, in <module>
table_html = ontology_table.render(fields=["title","domain","tags","expressivity"],rows=table_data["data"])
File "/usr/lib/python2.7/site-packages/Jinja2-2.7.3-py2.7.egg/jinja2/environment.py", line 969, in render
return self.environment.handle_exception(exc_info, True)
File "/usr/lib/python2.7/site-packages/Jinja2-2.7.3-py2.7.egg/jinja2/environment.py", line 742, in handle_exception
reraise(exc_type, exc_value, tb)
File "<template>", line 42, in top-level template code
File "/usr/lib/python2.7/site-packages/Jinja2-2.7.3-py2.7.egg/jinja2/environment.py", line 378, in getitem
return obj[argument]
jinja2.exceptions.UndefinedError: 'dict object' has no attribute 'tag'
In fact i'm loading a json string which contains a tags object like
{"tags": [{"tagObj": {"type": "uri", "value": "http://ci.emse.fr/opensensingcity/ns/sca/tourism"}, "tag": {"type": "literal", "value": "tourism"}}]}
and the jinja code below is failing with the stacktrace i provided:
{% for tag in row["tags"]%}
<span class="label label-info">{{tag["tag"]["value"] }}</span>
{% endfor %}
To iterate through a list of dictionaries in Jinja template with Python Flask, we use a for loop. to create the parent_list list of dicts. in our Jinja2 template to render the parent_list items in a for loop.
No, there is no way to inline Python into Jinja. However, you can add to the constructs that Jinja knows by extending the Environment of the template engine or the global namespace available to all templates. Alternately, you can add a filter that let's you format datetime objects.
Jinja, also commonly referred to as "Jinja2" to specify the newest release version, is a Python template engine used to create HTML, XML or other markup formats that are returned to the user via an HTTP response.
Jinja is a web template engine for the Python programming language. It was created by Armin Ronacher and is licensed under a BSD License. Jinja is similar to the Django template engine but provides Python-like expressions while ensuring that the templates are evaluated in a sandbox.
tag = {"tags": [{"tagObj": {"type": "uri", "value": "http://ci.emse.fr/opensensingcity/ns/sca/tourism"}, "tag": {"type": "literal", "value": "tourism"}}]}
you can get value using tag['tags'][0]['tag']['value']
and your out put will be 'tourism'
in this way.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With