Decode json and Iterate through items in django template

后端 未结 2 1975
孤街浪徒
孤街浪徒 2020-12-29 14:40

Hi I am using simplejson to import some json and then decode for use within a django template,

this is the decoded json:

{u\'ServerID\': 1, u\'Cache\         


        
2条回答
  •  轮回少年
    2020-12-29 15:20

    You don't need to do any further pre-processing in Python before sending the dictionary to Django (beyond what you have already done; using simplejson to parse the JSON string into a dictionary). Django's templating system is very good at dealing with dictionaries and lists.

    I will assume you have passed the dictionary to Django in a variable called obj.

    You should be able to access each item in the Result part of the dictionary using a for loop:

      {% for result in obj.Result %}
    • {{ result.Url }}, {{ result.PlaylistID }}, {{ result.Name }}
    • {% endfor %}

    For example, will place the results in a bulleted list, with the Url, Playlist and Name in a comma-separated list.

提交回复
热议问题