简体   繁体   中英

python layered dictionary to html table

I have been attempting to created a table in HTML from a python Dictionary that has an end result that has a stack ranking.

For example, a Manager can have many or one owners, each owner has many or one projects, and each project has a compliance score. Basically I want the name of the manager in the first column, all of the manager's owners in the second column, and then compliant in the third, non compliant in the fourth. Very similar to how a pivot table looks in excel

My view.py looks like this:

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

and my mangerView.html looks like this:

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

My issue is that each owner is printing across each row instead of stacking in a column.

Any guidance is much appreciated!

I was actually able to find the solution! This worked correctly

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM