【发布时间】:2018-05-30 07:10:02
【问题描述】:
大家好! 我有: 1) mongo 集合:
[{_id:ObjectId("5b0d5fb624d22e1b4843c06b")
collectionName:"collection0"
collectionCaption:"caption1"}
{_id:ObjectId("5b0d5fb824d22e1b4843d4c1")
collectionName:"collection1"
collectionCaption:"caption1"}
{_id:ObjectId("5b0d5fb924d22e1b4843d74a")
collectionName:"collection2"
collectionCaption:"caption1"}
{_id:ObjectId("5b0d5fb924d22e1b4843d7b0")
collectionName:"collection3"
collectionCaption:"caption1"}]
2) 带有视图的烧瓶应用:
def index():
a = mongo.db.collectionsNames.find()
return render_template('index.html', collectionsNames=a)
3) 模板:index.html 女巫扩展 base.html。 base.html:
{% extends "bootstrap/base.html" %}
{% block content %}
<div class="dropdown-menu" aria-labelledby="navbarDropdownMenuLink">
{% for asd in collectionsNames %}
<a class="dropdown-item" href="/{{ asd["collectionName"] }}">{{ asd["collectionCaption"] }}</a>
{% endfor %}
</div>
{% block contentBase %} {% endblock %}
{% endblock %}
index.html:
{% extends "base.html" %}
{% block contentBase %}
{% for zxc in collectionsNames %}
{{ zxc["collectionName"] }}
{% endfor %}
{% endblock %}
问题是:为什么基数和索引使用同一个集合变量,索引模板什么也没有显示?
但如果在视图中代码是:
def index():
a = mongo.db.collectionsNames.find()
b = mongo.db.collectionsNames.find()
return render_template('index.html', collectionsNames1=a, collectionsNames2=b)
在模板中我使用不同的变量,索引模板显示数据。
【问题讨论】:
-
如果在视图中函数代码是:a = [{"collectionName":"collection0", "collectionCaption":"col1"}, {"collectionName": "collection1", "collectionCaption": "col2"}, {"collectionName": "collection2", "collectionCaption": "col3"}, ] return render_template('index.html', collectionsNames=a) 然后索引模板显示数据