在我的視圖中,我創建了一個在背景關系中傳遞的變數,如下所示
{13: {112: 33.333333333333336, 120: 66.66666666666667, 125: 66.66666666666667}, 14: {110: 20.0, 111: 20.0, 113: 20.0, 121: 40.0, 126: 40.0}}
在我的模板中,我處于問題的回圈中,并希望將此值分配給問題中的答案:
{% for question in questions %}
<div class="ui basic padded segment left aligned">
<h4 class="ui header">
Question {{ forloop.counter }} / {{ questions|length }}: {{ question.prompt }}
</h4>
<ul>
{% for option in question.answer_set.all %}
<li> {{ forloop.counter }}) {{option.text}}:
{{ ans.{{ question.pk }}.{{ option.pk }} }} %.
{{ ans.13.120 }}
</li>
{% endfor %}
</ul>
</div>
{% endfor %}
如果我使用 {{ ans.13.120 }} 它可以作業,但不是動態的......我想要一種在 {{ }} 中使用變數的方法......類似于:{{ ans.(question.pk)。 (option.pk) }} ...
是否可以?
uj5u.com熱心網友回復:
如果您想在模板級別執行此操作,您可以為它制作自定義標簽并傳遞字典和鍵,然后您將獲得值
uj5u.com熱心網友回復:
您應該在視圖中完成這項作業。避免在模板中撰寫邏輯。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/355223.html
標籤:姜戈 模板 django-queryset
下一篇:為什么頁面上不顯示塊?
