Reputation: 451
How do I make the result of one expression evaluate to another in jinja2 templates? Something like:
{% for category in categories %}
<p>{{ category.{{ category.pk }} }}</p>
{% endfor %}
where category is a class object to evaluate to
<p>{{ category.news }}</p>
and then that to evaluate to something else like
<p>[list, of, items]</p>
Upvotes: 0
Views: 1518
Reputation: 451
I solved it by using []
in place of . as shown below
{% for category in categories %}
<p>{{ category[category.pk] }}</p>
{% endfor %}
Hint: given by Abhishek Kulkarni
this is because you can use .
or []
to get a class attribute as pointed out in the Jinja docs
Upvotes: 1