【问题标题】:python layered dictionary to html tablepython分层字典到html表
【发布时间】:2015-05-08 06:22:59
【问题描述】:

我一直在尝试从 python 字典中创建一个 HTML 表,该表的最终结果具有堆栈排名。

例如,经理可以有多个或一个所有者,每个所有者有多个或一个项目,每个项目都有一个合规分数。基本上我想要第一列中的经理姓名,第二列中所有经理的所有者,然后在第三列中符合要求,在第四列中不符合要求。与数据透视表在 excel 中的外观非常相似

我的 view.py 看起来像这样:

def ownerTable(request):
    managers = {}

    for m in Manager.objects.all():
        manager = m.login
        owners = {}
        for o in Owner.objects.filter(manager=m):
            projects = {}
            owner = o.login
            owners_compliant = 0
            owners_noncompliant = 0
            for p in DynamicProjectProperties.objects.filter(fieldValue=owner):
                project = p.project
                for pr in DynamicProjectProperties.objects.filter(project=project,fieldName='overall compliance'):
                    compliance = pr.overall_compliance
                    if compliance:
                        owners_compliant += 1
                    else:
                        owners_noncompliant += 1
            owners[owner] = owners_compliant,owners_noncompliant
        managers[manager] = owners
    print managers
    return render_to_response('managerView.html',{'managerDict':managers, 'ownersDict':owners})

我的 mangerView.html 看起来像这样:

<table>
<tr>
    <th id="managers"><strong>Managers</strong></th>
    <th id="owners"><strong>Project Owners</strong></th>
    <th id="noncompliant"><strong># of Non Compliant Projects</strong></th>
    <th id="compliant"><strong># of Compliant Projects</strong></th>
    <th id="total"><strong># of Projects</strong></th>
</tr>
{% for k,v in managerDict.items %}
<tr>
    <td header="managers">{{k}}</td>
    {% for attribute in v %}
        <td header="owners">{{attribute}}</td>
    {% endfor %}
</tr>
{% endfor %}
</table>

我的问题是每个所有者都在每一行打印,而不是在一列中堆叠。

非常感谢任何指导!

【问题讨论】:

    标签: python html dictionary html-table


    【解决方案1】:

    我实际上能够找到解决方案!这工作正常

    <table>
    <tr>
    	<th id="managers"><strong>Managers</strong></th>
    	<th id="owners"><strong>Project Owners</strong></th>
    	<th id="noncompliant"><strong># of Non Compliant Projects</strong></th>
    	<th id="compliant"><strong># of Compliant Projects</strong></th>
    	<th id="total"><strong># of Projects</strong></th>
    </tr>
    {% for k,v in managerDict.items %}
    <tr>
    	<td header="managers">{{k}}</td>
    	{% for attribute in v.items %}
    	<tr>
    		<td></td>
    		<td header="owners">{{attribute.0}}</td>
    		<td>{{attribute.1.0}}</td>
    		<td>{{attribute.1.1}}</td>
    	</tr>
    	{% endfor %}
    </tr>
    {% endfor %}
    </table>

    【讨论】:

      猜你喜欢
      • 2013-01-17
      • 2020-10-07
      • 1970-01-01
      • 2016-03-16
      • 2015-05-31
      • 2023-01-12
      • 1970-01-01
      • 2012-04-28
      • 2014-10-12
      相关资源
      最近更新 更多