Skip to main content
 首页 » 编程设计

python之使用 Flask 漂亮地显示 JSON 数据

2025年05月04日15kerrycode

<分区>

由于 GET,我得到了一个响应对象请求,我已将其转换为 JSON与 jsonify() .当我将其传递给模板时,我得到的只是一个 JSON 对象,例如:<Response 1366 bytes [200 OK]>这。

#request.py 
... 
response = requests.get('http://www.example.com') 
response_json = jsonify(all=response.text) 
 
return render_template( 
    'results.html', 
    form=ReqForm(request.form), 
    response=response_json, 
    date=datetime.datetime.now() 
) 

和模板..

#results.html 
... 
<div class="results"> 
    {{ response }} # --> gives <Response 1366 bytes [200 OK]> 
</div> 
... 

如何在模板中漂亮地显示此 JSON?