繁体   English   中英

如何在Jinja2中访问列表中的特定索引

[英]How to access specific index in list in jinja2

假设在jinja2中有一个列表a = [1,2,3,4],而我只想访问list的第三个索引。在其他语言中,我们编写了一个[2],但它在jinja2中显示了错误。

    def dashboard(request):
        user = request.user
        staff_detail = []
        staffs = models.Staff.objects.all()

        rec_total = models.Recordings.objects.all().count()
        rec_enquiry = models.Recordings.objects.filter(type=1).count()
        rec_complaint = models.Recordings.objects.filter(type=2).count()

        for staff in staffs:

            st = dict()
            st['staff_detail'] = staff
            st['total_recordings'] = staff.recordings_set.all().count()
            st['enquiry'] = staff.recordings_set.filter(type=1).count()
            st['complaint'] = staff.recordings_set.filter(type=2).count()
            staff_detail.append(st)
        return render(request, 'hackathon/index.html', {
            'staff_detail': staff_detail,
            'rec_total': rec_total,
            'rec_enquiry': rec_enquiry,
            'rec_complaint': rec_complaint,
            'staff_first': staff_detail[0],
        })

在HTML文件中只需要staff_detail的第一个元素当前我写

    {{staff_detail[0].staff_detail.name}}

但它显示错误我只能使用for循环访问它们

它应写为{{staff_detail.0.staff_detail.name}}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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