title: Use for loop variable as dictionary key in Django template
body: <p>I am trying to get the following working.</p>
  
  <p>The count loop needs to loop through for all values, and there may not be a user associated with each count, but the count value <code>i</code> needs to be used in each loop to pass to the JavaScript.</p>
  
  <p>python part:</p>
  
  <pre><code>users = {}
  users[1]={}
  users[1][id]=...
  users[1][email]=...
  
  ...
  
  count=[1,2,3,4,5,6,7,8,9,10]
  </code></pre>
  
  <p>Django template part:</p>
  
  <pre><code>{% for i in count %}
  do some stuff with the value i using {{i}} which always returns the value, i.e. 1
  
  email:{% if users.i.email %}'{{users.i.email}}'{% else %}null{% endif%}
  {% endfor %}
  </code></pre>
  
  <p>This returns nothing for email.
  When I substitute the number <code>1</code> for <code>i</code> in <code>{% if user.i.email %}</code> email returns the users email address.
  I'm using the data in JavaScript, so it needs to be implicitly null if it doesn't exist.
  I can't seem to get Django to recognize the <code>i</code> variable as a variable instead of the value i.</p>
  
  <p>using <code>[]</code> doesn't work, as it throws an invalid syntax error</p>
  
  <pre><code>email:{% if users.[i].email %}'{{users.[i].email}}'{% else %}null{% endif%}
  </code></pre>
  
  <p>I have tried using "<code>with</code>" statement</p>
  
  <pre><code>{% for i in count %}{% with current_user=users.i %}...
  </code></pre>
  
  <p>and then using <code>current_user.email</code>, but returned nothing</p>
  
  <p>Have also tried</p>
  
  <pre><code>{% for i in count %}{% with j=i.value %}...
  </code></pre>
  
  <p>just in case it would work, and then trying to use <code>j</code>, but same result.</p>
  
  <p>I have thought about creating an inner for loop that loops over the user object and check if i is equal to the key/value, but that seems expensive and not very scalable.</p>
  
  <p>Any ideas how I can force Django to view <code>i</code> as a variable and use it's value as an index, or any other way get around this?</p>
  
  <p>Thanks</p>
  
  <p>Jayd</p>
  
  <p>*<strong>Edit:</strong></p>
  
  <p>I tried the extra for loop, as suggested by Abhi, below.</p>
  
  <pre><code>{% for i in count %}
    {% for key, current_user in users.items %}
        do some stuff with the value i using {{i}} which always returns the value, i.e. 1
        email:{% if i == key and current_user.email %}'{{current_user.email}}'{% else %}null{% endif%}
    {% endfor %}
  {% endfor %}
  </code></pre>
  
  <p>This sort of works, but now it will repeat <code>do some stuff with the value i</code> for every value of users. and if I put in an if:</p>
  
  <pre><code>{% for i in count %}
    {% for key, current_user in users.items %}
      {% if i == key %}
        do some stuff with the value i using {{i}} which always returns the value, i.e. 1
        email:{% if i == key and current_user.email %}'{{current_user.email}}'{% else %}null{% endif%}
      {% endif%}
    {% endfor %}
  {% endfor %}
  </code></pre>
  
  <p>That ignores the loops when a count doesn't have a particular user.</p>
  
  <p>The only way I can see around this is the have the user loop at each place that I want to use <code>current_user</code>.</p>
  
  <pre><code>{% for i in count %}
        do some stuff with the value i using {{i}} which always returns the value, i.e. 1
        email:{% for key, current_user in users.items %}{% if i == key and current_user.email %}'{{current_user.email}}'{% else %}null{% endif%}{% endfor %}
  {% endfor %}
  </code></pre>
  
  <p>And this seems very expensive to do.
  Any ideas?</p>
  
  <p>I was thinking of maybe writing a filter that returns the values for users using <code>i</code> as the key:</p>
  
  <pre><code>{% with current_user=users|getuser:i %}
  </code></pre>
  
  <p>But I don't know if this will work or I will get the same error, where <code>i</code> is being passed as the value 'i' instead of a variable name.</p>
  
  <p>I will give it a try so long.</p>
  
  <p>*<strong>Edit</strong></p>
  
  <p>This didn't work.
  The  filter worked using <code>{{}}</code> returning the object, but it didn't work inside the <code>{% %}</code>
  .</p>
  
  <p>Thanks for the input</p>