Use variable as dictionary key in Django template
Posted
by CaptainThrowup
on Stack Overflow
See other posts from Stack Overflow
or by CaptainThrowup
Published on 2010-05-24T01:48:25Z
Indexed on
2010/05/24
1:50 UTC
Read the original article
Hit count: 392
django
|django-templates
I'd like to use a variable as an key in a dictionary in a Django template. I can't for the life of me figure out how to do it. If I have a product with a name or ID field, and ratings dictionary with indices of the product IDs, I'd like to be able to say:
{% for product in product_list %}
<h1>{{ ratings.product.id }}</h1>
{% endfor %}
In python this would be accomplished with a simple
ratings[product.id]
But I can't make it work in the templates. I've tried using with... no dice. Ideas?
© Stack Overflow or respective owner