Skip to main content
 首页 » 编程设计

python之Django 。如何在html tr>td>name/td>td>value/td>td>date/td>/tr>标签中显示json文件

2025年05月04日2298°冷暖

我想这是个愚蠢的问题,但我是 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"] 
]}` 

我的应用程序.html:

<li><a href="{%  url  'my_app.views.specific_document'  document.id  %}">{{ document.docfile.name }}</a></li> 

这是我的 View 函数:

    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") 

模板“specific_document.html”应该如何像我在主题中询问的那样显示数据?

请您参考如下方法:

为了得到表格,你应该放这样的东西:

<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),然后浏览该字典条目列表中的每个条目。