所以我将结果从ds_data = list(query.fetch())
传递到JINJA2:
return render_template('index.html',images=ds_data)
对于python,可以将其作为list进行迭代,但对于JINJA2则没有问题:
data-json="{{image|safe}}"
而不是HTML中的某些字典:'{}'
,但我也拥有完整的键:
data-json="<Entity('kind', 'id') {'someDictKey': 'value', 'someDictKey': 'value'}>"
嗯,关于如何在python中删除实体的键或在JINJA2中正确呈现它的任何想法?
模板片段:
{% block content %}
{% if images is defined %}
<div class="main-content" id="main-content">
...
{% for image in images %}
<tbody id="tbody{{ image['key'] }}" data-json="{{ image }}">
</tbody>
{% endfor %}
...
{% endif %}
{% endblock %}
谢谢!
json.dumps(image)
。具体来说,您会看到需要将image
强制转换为字典以获取所需的地图(https://googleapis.dev/python/datastore/latest/entities.html)。@app.template_filter('parse_entity_list')
def parse_filter(item):
# Recompose DS object to lists of dicts
a = json.dumps(item)
return a
app.jinja_env.filters['parse_entity_list'] = parse_filter
[JINJA2侧:
<tbody id="tbody{{ image['key'] }}" data-json="{{ image | parse_entity_list }}">