[Answered ]-Render python object to HTML – simple types but nested – in django

2πŸ‘

βœ…

the easiest approach would be translate your python object to JSON first.
then use one of many json to html converter.
EX:
https://github.com/bloopletech/json2html/tree/master

demo
http://json.bloople.net/#_output

because your python object include datetime type, the json decoder need to be extended first.

import json
def extend_json(obj):
    if isinstance(obj, date):
        return str(obj)
    elif isinstance(obj, datetime):
        return obj.strftime("%Y-%m-%d %H:%M:%S")
    elif isinstance(obj, Decimal):
        return float(obj)

data = json.dumps(obj, indent=4, sort_keys=True, default=extend_json)
πŸ‘€lucemia

Leave a comment