[英]How to print value of a "changing field" in Django template
我正在嘗試在模板中顯示字段列表(及其值)。 問題是字段名正在改變,我無法控制它們。 e,g 字段可以是:
field_name1
field_object1
field_input1
這里唯一不變的是前綴“field_”
這對在模板中獲取該字段的值提出了挑戰。 我遵循了這里的建議: Django:無法引用包含點並且能夠打印字段名稱的模板中的字段名稱。 但我仍然不知道如何打印這些字段的值。
模型.py
rowObj = {
'id': id,
'inputList': {'Input_' + k: v for k, v in objList}
}
# Example of the outputs (The outputs can be one of the following):
# rowObj = {
# 'id': 1,
# 'inputList': {'Input_Image' : 'http://test',
# 'Input_Image2' : 'http://test2' }
# }
#another time the output can be:
# rowObj = {
# 'id': 1,
# 'inputList': {'Input_Text' : 'Random text',
# 'Input_Text2' : 'Random text 2' }
# }
我的模板是:
{% for row in row_list %}
<tr>
<td>{{ row.id }}</td>
{% for obj in row.inputList %}
<td>{{ obj }}</td>
{% endfor %}
</tr>
{% endfor %}
上面的代碼只輸出字段名稱,即“Input_Image”和“Input_image2”我不知道如何打印這些字段的值。 你能幫忙嗎。 謝謝你。
reddit 上有人回答了這個問題。 解決方案是在模板中使用以下內容:
{% for k,v in row.inputList.items %} {{ k }} : {{ v }} {% endfor %}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.