【问题标题】:Display json along with html template in flask在烧瓶中显示 json 和 html 模板
【发布时间】:2017-08-15 01:29:32
【问题描述】:

如何在flask中返回一个json以及下面的html模板?

@app.route('/test')
def test():
    a = {
    "test1": flag,
    "test2": flag
}
    return '''
       <body>
       ...
       </body>
       '''

【问题讨论】:

  • 你不能。您只能退回其中之一。 HTML 的 Json。
  • 您希望得到的 HTTP 响应是什么样的?

标签: python html json flask


【解决方案1】:
@app.route('/test')
def test():
    a = {
    "test1": flag,
    "test2": flag
}
    b = jsonify(a)
    render_template("index.html",**locals())

在您的 html 文件中,您可以调用 a、b 等。

{{b}}

{{a}}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-11-04
    • 1970-01-01
    • 1970-01-01
    • 2018-12-16
    • 2016-05-14
    • 1970-01-01
    • 2020-03-11
    • 2020-07-23
    相关资源
    最近更新 更多