我建立了一个包含列表的字典。
我想按索引访问django模板中的列表
我使用自定义标签访问字典
@register.filter(name='keyvalue')
def keyvalue(dict, key):
return dict[key]
和html
{% for p in participant %}
<tr>
<td>{{ p }}</td>
<td>{{p.eMail}}</td>
<td>{{p.participantNationality}}</td>
<td>{{p.participantStreet}} {{p.participantStreetNumber}},
{{p.participantZipcode}}, {{p.participantCity}} </td>
<td>{{p.participantCountryResidency}}</td>
<td>{{p.participantSpecialNeeds}}</td>
<td>{{p.participant_fee_no_reduction}}€</td>
<td>
<p style="line-height : 10px">{{discounts|keyvalue:p.pk}}</p>
</td>
{% if totalAmount == 0 %}
<td>Free</td>
{% else %}
<td>{{p.participantFee}}€</td>
{% endif %}
</tr>
{% endfor %}
像这样的代码可以工作,它返回合适的参与者的合适列表,除了它是一个普通的python列表。
我想按索引访问列表中的值。
当我尝试
{{discounts.discount_list.1|keyvalue:p.pk}}
我的字符串索引超出范围,因为列表被视为字符串。
任何想法怎么做?谢谢