title: django template: custom template tag to display dictionary of dictionary
body: <p>I want to do this in my template:</p>
  
  <pre><code>{% for field in form %}
      {{ realNameDict.field.name }}
  {% endfor %}
  </code></pre>
  
  <p>realNameDict is a dictionary. The code above does not work. Why ?</p>
  
  <p><strong>EDIT:</strong> 
  In python, I think I could do this : </p>
  
  <pre><code>print realNameDict[field[name]]
  </code></pre>
  
  <p>Many thanks.</p>
  
  <h1><strong>SOLUTION:</strong></h1>
  
  <p>Thanks to the explanation of @Calvin Cheng:
  The trick is to use custom template tags.</p>
  
  <p>1/ In your application directory, create a directory called <code>templatetags</code>. Create two files: <code>__init__.py</code> (leave it empty) and <code>extra.py</code> (used to create the custom template tags).</p>
  
  <p>2/ In extra.py, create the function to get the value of a dictionary of dictionary :</p>
  
  <pre><code>from django import template
  register = template.Library()
  
  @register.filter
  def ofKey(value, arg):
      if value:
          return value.get(arg)
      else:
          return ""
  </code></pre>
  
  <p>3/In your template, inside the content block, add the following line to load the custom template tag:</p>
  
  <pre><code>{% load extra %}
  </code></pre>
  
  <p>4/ Call the custom template tag in your template:</p>
  
  <pre><code>{{ realNameDict|ofKey:field.name }}
  </code></pre>