将 python 字典传递给模板
Passing python dict to template
一定有办法做到这一点...但我找不到。
如果我像这样将一本字典传递给模板:
@app.route("/")
def my_route():
content = {'thing':'some stuff',
'other':'more stuff'}
return render_template('template.html', content=content)
这在我的模板中工作正常...但是有什么方法可以删除 'content.' ,来自
{{ content.thing }}
我觉得我以前见过这个,但在任何地方都找不到。有什么想法吗?
尝试
return render_template('template.html', **content)
您需要使用 **
运算符将 content
字典作为关键字参数传递:
return render_template('template.html', **content)
这实际上与将字典中的项目作为关键字参数传递相同:
return render_template(
'template.html',
thing='some stuff',
other='more stuff',
)
一定有办法做到这一点...但我找不到。
如果我像这样将一本字典传递给模板:
@app.route("/")
def my_route():
content = {'thing':'some stuff',
'other':'more stuff'}
return render_template('template.html', content=content)
这在我的模板中工作正常...但是有什么方法可以删除 'content.' ,来自
{{ content.thing }}
我觉得我以前见过这个,但在任何地方都找不到。有什么想法吗?
尝试
return render_template('template.html', **content)
您需要使用 **
运算符将 content
字典作为关键字参数传递:
return render_template('template.html', **content)
这实际上与将字典中的项目作为关键字参数传递相同:
return render_template(
'template.html',
thing='some stuff',
other='more stuff',
)