在Django模板中创建HTML/JS矩阵

2024-05-19 11:30:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个以字典形式返回此数据的视图:

{('ACCOUNTS', 'Staff'): 8, ('ACCOUNTS', 'TOA'): 3, ('HR', 'Officer'): 4, ('HR', 'Staff'): 1}

我需要将这个字典转换成django模板中的矩阵。大概是这样的:

           Staff     Officer      TOA
ACCOUNTS    8           0          3

HR          1           4          0

请注意,这本词典可能再也找不到了

models.py:

class EmployeeMealRequest(models.Model):

    Dept = models.CharField(max_length=35, blank=True, null=True)
    shift = models.CharField(max_length=5, blank=True,null=True)
    EmpCategory = models.CharField(max_length=15, blank=True, null=True)

views.py:

def reports(request, download=None):
    rows = EmployeeMealRequest.objects.all().order_by('Dept')
    myFilter = EmployeeMealRequestFilter(request.GET, queryset=rows)
    rows = myFilter.qs
    context = {
        'rows': rows,
        'filter': myFilter,

    }
    if download == str(1):
        data = EmployeeMealRequest.objects.filter(
            Att_date__gt=today).values(
                'EmpCategory',
                'Dept',).annotate(
                    total=Count('Dept')
                )
        d = {}
        categories = []
        depatments = []
        for i in data:
            d[( i.get('Dept'), i.get('EmpCategory') )] = i.get('total')
        context['data'] = d


    return render(request, 'account/download.html', context)

Tags: truemodelsrequesthrnulllengthmaxrows

热门问题