Django templates and variable attributes
<p>I'm using Google App Engine and Django templates.<br>
I have a table that I want to display the objects look something like:<br></p>

<pre><code>Object Result:
    Items = [item1,item2]
    Users = [{name='username',item1=3,item2=4},..]
</code></pre>

<p>The Django template is:</p>

<pre><code>&lt;table&gt;
&lt;tr align="center"&gt;
    &lt;th&gt;user&lt;/th&gt;
    {% for item in result.items %}
        &lt;th&gt;{{item}}&lt;/th&gt;
    {% endfor %}
&lt;/tr&gt;

{% for user in result.users %}
    &lt;tr align="center"&gt; 
        &lt;td&gt;{{user.name}}&lt;/td&gt;
        {% for item in result.items %}
            &lt;td&gt;{{ user.item }}&lt;/td&gt;
        {% endfor %}
    &lt;/tr&gt;
{% endfor %}
&lt;/table&gt;
</code></pre>

<p>Now the <a href="http://www.djangoproject.com/documentation/0.96/templates/#variables" rel="nofollow">Django documention</a> states that when it sees a <strong>.</strong> in variables<br>
It tries several things to get the data, one of which is dictionary lookup which is exactly what I want but doesn't seem to happen...</p>

<p>I found a "nicer"/"better" solution for getting variables inside
Its not the nicest way, but it works.</p>

<p>You install a custom filter into django which gets the key of your dict as a parameter</p>

<p>To make it work in google app-engine you need to add a file to your main directory,
I called mine *django_hack.py* which contains this little piece of code</p>

<pre><code>from google.appengine.ext import webapp

register = webapp.template.create_template_register()

def hash(h,key):
    if key in h:
        return h[key]
    else:
        return None

register.filter(hash)
</code></pre>

<p>Now that we have this file, all we need to do is tell the app-engine to use it...
we do that by adding this little line to your main file</p>

<pre><code>webapp.template.register_template_library('django_hack')
</code></pre>

<p>and in your template view add this template instead of the usual code</p>

<pre><code>{{ user|hash:item }}
</code></pre>

<p>And its should work perfectly =)</p>
