How do I return JSON without using a template in Django?

前端 未结 9 1580
再見小時候
再見小時候 2020-12-04 07:01

This is related to this question: Django return json and html depending on client python


I have a command line Python API for a Django app. When I access the a

9条回答
  •  隐瞒了意图╮
    2020-12-04 07:35

    In the case of the JSON response there is no template to be rendered. Templates are for generating HTML responses. The JSON is the HTTP response.

    However, you can have HTML that is rendered from a template withing your JSON response.

    html = render_to_string("some.html", some_dictionary)
    serialized_data = simplejson.dumps({"html": html})
    return HttpResponse(serialized_data, mimetype="application/json")
    

提交回复
热议问题