JINJA2中的GCP数据存储渲染也显示了完整的密钥

问题描述 投票:0回答:2

所以我将结果从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 %}

谢谢!

python-3.x google-cloud-datastore jinja2
2个回答
1
投票
您实际上必须将数据转换为json字符串。例如。 json.dumps(image)。具体来说,您会看到需要将image强制转换为字典以获取所需的地图(https://googleapis.dev/python/datastore/latest/entities.html)。

0
投票
毕竟这是我昨晚所做的:

@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 }}">

© www.soinside.com 2019 - 2024. All rights reserved.