-1

I have django template and I get a some object. I want apply for in cycle for all atributes this object.

    {% for point in Object %}
        <h1>{{ Object[point] }}</h1>
    {% endfor %}
  • who upvoted this question? there are like 100 answers out there explaining what you need to do – hansTheFranz Nov 10 '17 at 14:12
  • Pretty certain this is just a duplicate of https://stackoverflow.com/questions/2217478/django-templates-loop-through-and-print-all-available-properties-of-an-object – Tiger_Mike Nov 10 '17 at 14:16
  • 1
    @hansTheFranz maybe say at least one? –  Nov 10 '17 at 14:25

2 Answers2

0

You can't do that in the template. One solution is to convert your object in a dictionary in the view with object.__dict__ and then iterate over it with:

{% for attr, value in object_dict.items %}
    {{ attr }} : {{ value }}
{% endfor %}

As py_dude suggests, you can discard attributes starting with underscores to keep your actual attributes.

Cyrlop
  • 1,894
  • 1
  • 17
  • 31
-1
{% for k, v in Object.__dict__.items() %}
    {% if not k.startswith("_") %}
        <h1>{{ v }}</h1>
    {% endif %}
{% endfor %}
py_dude
  • 822
  • 5
  • 13