Reputation: 123
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
Upvotes: 3
Views: 8280
Reputation: 123
It should be written as {{staff_detail.0.staff_detail.name}}
Upvotes: 6