Flask Python - 如何在 json 格式和 return 格式上创建换行符 html?

Flask Python - How do make a newline on json format and return it as html?

我一直在尝试使用 JSON 格式换行,但 none 行不通。

from flask import *
import json

app = Flask(__name__)


@app.route("/user/", methods=["GET"])
def user():
    datajson = {"Author": "Stawa", 
                "Version": "0.1.7"}
    json_format = json.dumps(datajson, indent=6)
    return render_template("index.html", json_format=json_format)

在index.html

<!DOCTYPE html>
<html lang="en">
   <head>
      <title>TEST</title>
      <meta charset="utf-8">
   </head>
   <body>
      <p>{{ json_format }}</p>
   </body>
</html>

输出是{"Author": "Stawa", "Version": "0.1.7"},但我想把它变成

{
 "Author": "Stawa",
 "Version": "0.1.7"
}

这是因为浏览器将您的 JSON 字符串解释为 HTML,并且它不关心换行符。要保留格式,您可以使用 HTML <pre> 标签。我用您的代码测试了以下模板,它在两行中显示了 JSON。希望它也适用于您的计算机!

<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
   <head>
      <title>TEST</title>
      <meta charset="utf-8">
   </head>
   <body>
      <pre>{{ json_format }}</pre>
   </body>
</html>

输出: