You need to create your own filter that will work like getattr in python and use it in a template:
{{ object|getattribute:field }}
Here's how to do it: Performing a getattr () style lookup in a django template
But I do not think this is a really good idea. Try to make this logic in the view, for example:
object_values = [] for object in objects object_values.append([]) for field in fields: object_values[-1].append(getattr(object, field)) return render_to_response(template, {'object_values': object_values}, context_instance=RequestContext(request))
and in the template:
<table> {% for values in object_values %} <tr> {% for value in values %} <td> {{ value }} </td> {% endfor %} </tr> {% endfor %} </table>
The Django template system does not provide many functions (filters) because you have to do all the logic in the views. The template should contain only data.
gruszczy
source share