当我与此同时使用任何数字时,我也收到此错误,并且我在此站点上看到了一些解决方案,但是我无法解决,或者我做错了方法。 帮助将不胜感激。谢谢
resend_lst的样本数据:['0','1','0','0']
django.template.exceptions.TemplateSyntaxError:无法解析“ resend_lst [0]”中的其余部分:“ [0]”
{% for attendee_data in attendees_data %}
<tr>
<td>{{ attendee_data.attendee.name }}</td>
<td>{{ attendee_data.attendee.email }}</td>
<td>{{ attendee_data.attendee.mobile }}</td>
<td>{{ attendee_data.attendee_response.date }}</td>
</tr>
**resend_lst is a list data type and I need to access this with its index in that loop **
{% if resend_lst[{{forloop.counter}}] == '0' %}
<style>
#response-{{forloop.counter}}{
display:none;
}
#cancel-{{forloop.counter}}{
display:none;
}
#loader-next-{{forloop.counter}}{
display:none;
}
#ajax-loader{
text-align: center;
width:19px;
height:19px;
}
</style>
{% else %}
<style>
#loader-next-{{forloop.counter}}{
display:none;
}
#ajax-loader{
text-align: center;
width:19px;
height:19px;
}
</style>
{% endif %}
<-- some task -->
{% endfor %}
答案 0 :(得分:1)
我的建议是在Python后端建立一个同时包含attendees_data
和resend_lst
的新列表,但是,如果您仍然想在Jinja
模板中这样做,则可以通过嵌套循环实现此目标,这对性能不利。
python部分
for i in range(len(attendees_data)):
attendees_data['style_way'] = resend_data[i]
模板部分
{% for attendee_data in attendees_data %}
<tr>
<td>{{ attendee_data.attendee.name }}</td>
<td>{{ attendee_data.attendee.email }}</td>
<td>{{ attendee_data.attendee.mobile }}</td>
<td>{{ attendee_data.attendee_response.date }}</td>
</tr>
**resend_lst is a list data type and I need to access this with its index in that loop **
{% if attendee_data.style_way == 0 %}
<style>
#response-{{forloop.counter}}{
display:none;
}
#cancel-{{forloop.counter}}{
display:none;
}
#loader-next-{{forloop.counter}}{
display:none;
}
#ajax-loader{
text-align: center;
width:19px;
height:19px;
}
</style>
{% else %}
<style>
#loader-next-{{forloop.counter}}{
display:none;
}
#ajax-loader{
text-align: center;
width:19px;
height:19px;
}
</style>
{% endif %}
<-- some task -->
{% endfor %}
使用loop.count作为列表中元素和for循环的索引。
{% for attendee_data in attendees_data %}
{% set outer_loop = loop %}
<tr>
<td>{{ attendee_data.attendee.name }}</td>
<td>{{ attendee_data.attendee.email }}</td>
<td>{{ attendee_data.attendee.mobile }}</td>
<td>{{ attendee_data.attendee_response.date }}</td>
</tr>
**resend_lst is a list data type and I need to access this with its index in that loop **
{% for resend_data in resend_lst %}
{% if loop.count == outer_loop.count and resend_data == 0 %}
<style>
#response-{{forloop.counter}}{
display:none;
}
#cancel-{{forloop.counter}}{
display:none;
}
#loader-next-{{forloop.counter}}{
display:none;
}
#ajax-loader{
text-align: center;
width:19px;
height:19px;
}
</style>
{% else %}
<style>
#loader-next-{{forloop.counter}}{
display:none;
}
#ajax-loader{
text-align: center;
width:19px;
height:19px;
}
</style>
{% endif %}
<-- some task -->
{% endfor %}