gpt4 book ai didi

python - 无法将从 MongoDB 检索到的 json 值返回到 python flask 中的 HTML 页面

转载 作者:行者123 更新时间:2023-12-05 07:03:28 26 4
gpt4 key购买 nike

我能够从 MongoDB 单独检索所需的值,也能够打印相同的值,但无法将所有这些值返回到 HTML 页面。当我尝试返回这些值时,尽管我在 jinja 中使用了 for 循环,但我只能看到第一个值。
我的代码如下:

@app.route('/webhookdisplay', methods=['POST', 'GET'])
def webhooksdis():
collection10 = db['webhooks']
a = collection10.find({"name": "abc"}, {'_id': 0, 'recorded_at':0, 'expiry_time': 0, 'version': 0, 'created_at': 0, 'account_id': 0, 'device_id': 0})
for i in collection10.find({}):
d = i.get('data', {}).get('geometry', {}).get('coordinates')
print(d)
name = i.get('data', {}).get('geofence_metadata', {}).get('name')
print(name)
return render_template("webhooks.html", name = name, a = a, d =d)

在上面的代码名称中也有 None 值,所以当我尝试单独返回它时显示 TypeError: 'NoneType' object is not iterable。假设如果我返回那些没有 None 值的值,它会返回但只显示第一个值。

HTML 代码

{% for i in d %}
{{ i }}
{% endfor %}<br />

这个 jinja 适用于所有值,但我需要在 flask 中检索的单个值:

<table>   
<th>
{% for item in a %}
</th>
<tr>
<td><th> {% for key, value in item.items() %} </th> </td>
<td><span>{{ key }} : {{ value }}</span> </td>
<br />
<td>{% endfor %}</td>
<td>{% endfor %}</td>
</tr>
</table>


**预期输出**应该返回所有值,包括 None 值,我在 flask 中检索这些值,以便我可以在 HTML 页面中呈现

最佳答案

您可以使用 jinja用于模板渲染。

使用下面的伪代码,您可以填充 JSON 数据:

{% for key, value in a %}        
<span>{{key}} : {{value}}</span>
{% endfor %}

关于python - 无法将从 MongoDB 检索到的 json 值返回到 python flask 中的 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63207570/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com