How to correctly use Flask's jsonify() to return json?

前端 未结 1 1358
滥情空心
滥情空心 2021-01-20 19:45

I\'m having a little trouble using the flask.jsonify function to output a formatted json response from a dictionary input, as described in here.

My code is seems to

相关标签:
1条回答
  • 2021-01-20 20:36

    You can use json.dumps like so:

    @app.route('/')
    def home():
    return render_template(
        'index.html',
        title='Home Page',
        result=json.dumps({"a":[{"o":1},{"o":2}]}, sort_keys = False, indent = 2)
    )
    

    and just format it in the template like so:

    {% if result %}
       <pre>{{ result }}</pre>
    {% endif %}
    

    If this fits to your expectations. I think that jsonify is used to provide http.response data, not context data for templates.

    Have a look here for jsonify: https://stackoverflow.com/a/13172658/1307985

    0 讨论(0)
提交回复
热议问题