I am trying to display a list of fields (and their values) in a template. The problem is that the fieldnames are changing and I dont have control over them. e,g the fields can be:
field_name1
field_object1
field_input1
The only thing constant here is the prefix "field_"
This makes a challenge to get the value of that field in the template. I followed the advice here: Django: Cannot reference a fieldname in template which contains a dot and was able to print the field names. But i still dont know how to print the values of these fields.
model.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' }
# }
My template is:
{% for row in row_list %}
<tr>
<td>{{ row.id }}</td>
{% for obj in row.inputList %}
<td>{{ obj }}</td>
{% endfor %}
</tr>
{% endfor %}
The above code only outputs the field name which would be "Input_Image" and "Input_image2" I dont know how to print the values of these fields. Can you please help. Thank you.
Someone on reddit answered this question. The solution is using the following in template:
{% for k,v in row.inputList.items %} {{ k }} : {{ v }} {% endfor %}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.