# Access dict value in template using variable as key

**URL:** <https://forum.djangoproject.com/t/access-dict-value-in-template-using-variable-as-key/28907>\
**Category:** Templates & Frontend\
**Created:** [March 9, 2024, 10:36pm UTC](https://forum.djangoproject.com/t/access-dict-value-in-template-using-variable-as-key/28907 "2024-03-09T22:36:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![probso](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/probso/32/13956_2.png) [@probso](https://forum.djangoproject.com/u/probso)\
**Post date:** [March 9, 2024, 10:36pm UTC](https://forum.djangoproject.com/t/access-dict-value-in-template-using-variable-as-key/28907/1 "2024-03-09T22:36:44Z")

</div>

Within a template, how do I access a dictionary value by specifying a key that is stored in a variable?

If I have a dictionary e.g.

```auto
eventEntriesDict	
{1: 'A',
 2: 'B',
 3: 'C',
 4: 'D',}

```

I can access any value from within the template by:

```auto
{{ eventEntriesDict.2 }}

```

This returns the expected value of B

What I would like to do however is

```auto
{% with myid = query.field.id %}
{{ eventEntriesDict.myid }}
{% endwith %}

```

If I try this I don’t get anything returned.

I’d be grateful for advice on where I’m going wrong.

Many thanks

---

<div class="post-metadata">

**Author:** ![KenWhitesell](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/kenwhitesell/32/280_2.png) [@KenWhitesell](https://forum.djangoproject.com/u/KenWhitesell)\
**Post date:** [March 9, 2024, 11:47pm UTC](https://forum.djangoproject.com/t/access-dict-value-in-template-using-variable-as-key/28907/2 "2024-03-09T23:47:37Z")

</div>

You’re not doing anything wrong. The Django template language is not designed to handle that.

You need to either reorganize your data in the view, create a custom filter, or create a custom model method to it.
