在Jinja中打印字典值
我正在把一个字典传递给Jinja模板,像这样:
locations = {1: "Baltimore, MD"}
我希望值能显示成这样:
Baltimore, MD
但是,当我在Jinja模板中打印这个值时,它看起来是这样的:
{'Baltimore, MD'}
这是我在模板中的代码:
<td class="col-md-2">{{ locations[check.location_id] | string }}</td>
我试着在 locations[check.location_id]
的后面加上 [0]
,但没有用。
这是怎么回事呢?
1 个回答
3
你需要提供一个完整的例子,因为根据给出的信息推测,代码运行得很好。这里有一个例子。
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def hello_world():
check = {'location_id': 1}
locations = {1: "Baltimore, MD"}
return render_template('example.html', locations=locations, check=check)
if __name__ == '__main__':
app.run()
然后是 example.html
:
{{ locations[check.location_id] }}
显示:
Baltimore, MD
在渲染后的模板中。