[英]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. 假设在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],
})
In html file want only the 1st element of staff_detail currently I write 在HTML文件中只需要staff_detail的第一个元素当前我写
{{staff_detail[0].staff_detail.name}}
but it is showing error I can only access them using for loop 但它显示错误我只能使用for循环访问它们
它应写为{{staff_detail.0.staff_detail.name}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.