我想,这是个愚蠢的问题,但我是json的新人,所以任何一个人都会帮忙。
我有json文件:
`{"aaData": [
[1, "70.1700", "2008-12-29 11:23:00"],
[2, "70.2600", "2008-12-29 16:22:00"],
[3, "70.6500", "2008-12-30 11:30:00"],
[4, "70.8700", "2008-12-30 16:10:00"],
[5, "70.5500", "2009-01-02 11:09:00"],
[6, "70.6400", "2009-01-02 16:15:00"],
[7, "70.6500", "2009-01-05 11:17:00"]
]}`
my_app.html:
<li><a href="{% url 'my_app.views.specific_document' document.id %}">{{ document.docfile.name }}</a></li>
这是我的视图功能:
def specific_document(request, document_id=1):
json_file = Document.objects.get(id=document_id).docfile.url
mydata = json.loads(json_file)
return render(request, 'specific_document.html', {"mydata": mydata, "documents": documents}, content_type="application/xhtml+xml")
模板&#39; specific_document.html&#39;应该像我在主题中那样显示数据吗?
答案 0 :(得分:1)
为了得到这个表,你应该写下这样的东西:
<table>
{% for name, items in mydata.iteritems %}
<!-- name = 'aaData' and items = [ list of entries ] -->
{% for item in items %}
<!-- item is ["number", "value", "date"] and you can access them by index -->
<!-- Name -->
<tr>
<td>{{name}}</td>
<!-- Value -->
<td>{{item.1}}</td>
<!-- Date -->
<td>{{item.2}}</td>
</tr>
{% endfor %}
{% endfor %}
</table>
在这里,我首先浏览字典(for name, items in mydata.iteritems
),然后浏览该字典条目列表中的每个条目。