简体   繁体   中英

How to access specific index in list in jinja2

Suppose there is list a=[1,2,3,4] in jinja2 and i want to access only 3rd index of list .In other languages we write a[2] but it shows error in 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],
        })

In html file want only the 1st element of staff_detail currently I write

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

but it is showing error I can only access them using for loop

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

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