Iterate over object in Jinja2?

google-app-engine, jinja2

Solution

This will do the trick in simple python code:

for attribute in record.properties():
    print '%s: %s' % (attribute, getattr(record, attribute))

You can put the getattr function in the context so you can call it in jinja2 as shown below:

{% for record in records %}
    <tr>
    {% for attribute in record.properties() %}
        <td>{{ getattr(record, attribute) }}</td>
    {% endfor %}
    </tr>
{% endfor %}

Problem

I'm using Jinja2 on Google App Engine. I have a ListView which renders a generic template. At the moment, I'm not sure exactly what I want to display, so I just want to display each attribute of the model. Is there a way to iterate over the object to output each one in a table cell? For example: ``` {% for record in records %} <tr> {% for attribute in record %} <td>{{ attribute }}</td> {% endfor %} </tr> {% endfor %} ``` Any advice appreciated. Thanks

Original source