Home>

Thanks for your help.
I want to output the following JSON to HTML with Python (Django), but I don't know how to extract the JSON.

JSON:
[{title: "Rashomon", author: "Ryunosuke Akutagawa", price: 500}, {title: "Botchan", author: "Natsume Soseki", price: 500}]

HTML:
{% for data in datas%}
{{data.title}}
{{data.author}}
{{data.price}}
{% endfor%}

I want to implement using a template like this, but the template variable is not specified correctly and nothing is displayed on the screen. How should I define it? Thank you.

  • Answer # 1

    I made it by the method I usually presented, so I will write it for the time being.

    # views.py
    import json
    from django.shortcuts import render
    def index (request):
        data ='[{" title": "Rashomon", "author": "Ryunosuke Akutagawa", "price": 500}, {"title": "Bocchan", "author": "Natsume Soseki", " price ": 500}]'
        datas = json.loads (data)
        return render (request, "index.html", {"datas": datas})
    <!-index.html->
    {% for data in datas%}
      {{data.title}}
      {{data.author}}
      {{data.price}}
    {% endfor%}

    I think it's good to check if you can return it properly with json
    Please note that if you send it as text, you need to process it with javascript.